vfven commited on
Commit
99f40e1
·
verified ·
1 Parent(s): 38c0f68

Create core/file_builder.py

Browse files
Files changed (1) hide show
  1. core/file_builder.py +76 -0
core/file_builder.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # core/file_builder.py
2
+ from pathlib import Path
3
+ from io import BytesIO
4
+ from datetime import datetime
5
+ from docx import Document
6
+ from docx.shared import Inches, Pt, RGBColor
7
+ from docx.enum.text import WD_ALIGN_PARAGRAPH
8
+ import openpyxl
9
+ from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
10
+
11
+ DOCS_DIR = Path("data/docs")
12
+ DOCS_DIR.mkdir(parents=True, exist_ok=True)
13
+
14
+ def build_docx(title: str, content: str, images: list[bytes] = None) -> Path | None:
15
+ doc = Document()
16
+
17
+ # Configuración básica
18
+ section = doc.sections[0]
19
+ section.top_margin = section.bottom_margin = Inches(1)
20
+ section.left_margin = section.right_margin = Inches(1.2)
21
+
22
+ # Título
23
+ p = doc.add_heading(title, 0)
24
+ p.alignment = WD_ALIGN_PARAGRAPH.CENTER
25
+ if p.runs:
26
+ run = p.runs[0]
27
+ run.font.color.rgb = RGBColor(0x1a, 0x56, 0xdb)
28
+
29
+ # Fecha
30
+ doc.add_paragraph(f"Generado por Mission Control AI — {datetime.now().strftime('%B %d, %Y')}", style='Italic')
31
+ doc.add_paragraph()
32
+
33
+ # Procesar contenido markdown-like
34
+ lines = content.splitlines()
35
+ current_p = None
36
+
37
+ for line in lines:
38
+ line = line.strip()
39
+ if not line:
40
+ continue
41
+ if line.startswith('## '):
42
+ doc.add_heading(line[3:], level=1)
43
+ elif line.startswith('### '):
44
+ doc.add_heading(line[4:], level=2)
45
+ elif line.startswith('- ') or line.startswith('* '):
46
+ p = doc.add_paragraph(line[2:], style='List Bullet')
47
+ else:
48
+ if current_p is None:
49
+ current_p = doc.add_paragraph()
50
+ current_p.add_run(line + " ")
51
+
52
+ # Insertar imágenes si existen
53
+ if images:
54
+ for img_bytes in images:
55
+ try:
56
+ doc.add_picture(BytesIO(img_bytes), width=Inches(5))
57
+ last_p = doc.paragraphs[-1]
58
+ last_p.alignment = WD_ALIGN_PARAGRAPH.CENTER
59
+ except:
60
+ pass
61
+
62
+ # Pie de página
63
+ p = doc.add_paragraph("— Generado por Mission Control AI —")
64
+ p.alignment = WD_ALIGN_PARAGRAPH.CENTER
65
+ if p.runs:
66
+ p.runs[0].italic = True
67
+ p.runs[0].font.size = Pt(9)
68
+ p.runs[0].font.color.rgb = RGBColor(0x6b, 0x72, 0x80)
69
+
70
+ # Guardar
71
+ safe_title = "".join(c for c in title if c.isalnum() or c in " -_")[:40]
72
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
73
+ filename = DOCS_DIR / f"{safe_title}_{timestamp}.docx"
74
+
75
+ doc.save(filename)
76
+ return filename