Spaces:
Sleeping
Sleeping
| """ | |
| Formatadores de parágrafos. | |
| """ | |
| from docx import Document | |
| from docx.shared import Pt, Inches, Cm, RGBColor | |
| from docx.enum.text import WD_ALIGN_PARAGRAPH | |
| from typing import Optional, Tuple, Union | |
| def aplicar_cor_run(run, cor: Optional[Union[Tuple[int, int, int], RGBColor]]) -> None: | |
| """ | |
| Aplica cor RGB a um run. | |
| Args: | |
| run: Run do python-docx | |
| cor: tuple (r,g,b) ou RGBColor | |
| """ | |
| if cor: | |
| if isinstance(cor, tuple): | |
| run.font.color.rgb = RGBColor(cor[0], cor[1], cor[2]) | |
| else: | |
| run.font.color.rgb = cor | |
| def criar_paragrafo_formatado( | |
| doc: Document, | |
| texto: str, | |
| negrito: bool = False, | |
| sublinhado: bool = False, | |
| italico: bool = False, | |
| tamanho: int = 11, | |
| cor: Optional[Union[Tuple[int, int, int], RGBColor]] = None, | |
| alinhamento=WD_ALIGN_PARAGRAPH.JUSTIFY, | |
| espaco_antes: int = 0, | |
| espaco_depois: int = 6, | |
| recuo_esq: float = 0, | |
| recuo_primeira_linha: float = 0, | |
| fonte: str = 'Arial' | |
| ): | |
| """ | |
| Cria um parágrafo com formatação completa. | |
| Args: | |
| doc: Documento python-docx | |
| texto: Texto do parágrafo | |
| negrito, sublinhado, italico: Formatação do texto | |
| tamanho: Tamanho da fonte em pontos | |
| cor: RGBColor ou tuple (r,g,b) | |
| alinhamento: WD_ALIGN_PARAGRAPH constant | |
| espaco_antes, espaco_depois: Espaçamento em pontos | |
| recuo_esq: Recuo esquerdo em inches | |
| recuo_primeira_linha: Recuo da primeira linha em cm | |
| fonte: Nome da fonte | |
| Returns: | |
| Parágrafo criado | |
| """ | |
| p = doc.add_paragraph() | |
| run = p.add_run(texto) | |
| run.bold = negrito | |
| run.underline = sublinhado | |
| run.italic = italico | |
| run.font.size = Pt(tamanho) | |
| run.font.name = fonte | |
| aplicar_cor_run(run, cor) | |
| p.alignment = alinhamento | |
| p.paragraph_format.space_before = Pt(espaco_antes) | |
| p.paragraph_format.space_after = Pt(espaco_depois) | |
| if recuo_esq: | |
| p.paragraph_format.left_indent = Inches(recuo_esq) | |
| if recuo_primeira_linha: | |
| p.paragraph_format.first_line_indent = Cm(recuo_primeira_linha) | |
| return p | |
| def add_body_text(doc: Document, text: str, cor=None, indent: bool = True): | |
| """Adiciona texto do corpo com formatação padrão.""" | |
| return criar_paragrafo_formatado( | |
| doc, text, tamanho=11, cor=cor, | |
| espaco_depois=6, recuo_esq=0.3 if indent else 0, | |
| recuo_primeira_linha=1.25 if indent else 0 | |
| ) | |
| def add_placeholder_text(doc: Document, text: str = "[Preencher informações]", indent: bool = True): | |
| """Adiciona texto placeholder em vermelho.""" | |
| return criar_paragrafo_formatado( | |
| doc, text, tamanho=11, cor=RGBColor(255, 0, 0), | |
| espaco_depois=6, recuo_esq=0.3 if indent else 0, | |
| recuo_primeira_linha=1.25 if indent else 0 | |
| ) | |
| def add_bullet_text(doc: Document, text: str, indent_level: int = 1): | |
| """Adiciona texto com bullet/recuo.""" | |
| return criar_paragrafo_formatado( | |
| doc, f"• {text}", tamanho=11, | |
| espaco_depois=3, recuo_esq=0.3 + (indent_level * 0.2) | |
| ) | |