Corin1998 commited on
Commit
8f18cb2
·
verified ·
1 Parent(s): be9db67

Update modules/exporters.py

Browse files
Files changed (1) hide show
  1. modules/exporters.py +64 -45
modules/exporters.py CHANGED
@@ -1,55 +1,74 @@
 
1
  from pathlib import Path
 
 
2
  from docx import Document
3
  from pptx import Presentation
 
 
 
 
 
4
 
5
- EXPORT_DIR = Path("data")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def export_docx(proposal_markdown: str, company_name: str) -> str:
8
- EXPORT_DIR.mkdir(parents=True, exist_ok=True)
9
  doc = Document()
10
- doc.add_heading(f"提案書({company_name}向け)", level=1)
11
- for line in proposal_markdown.splitlines():
12
- if line.startswith("#"):
13
- doc.add_heading(line.lstrip("# ").strip(), level=2)
 
14
  else:
15
  doc.add_paragraph(line)
16
- path = EXPORT_DIR / f"proposal_{company_name}.docx"
17
- doc.save(path)
18
- return str(path)
 
 
 
 
 
 
19
 
20
- def export_pptx(proposal_markdown: str, company_name: str) -> str:
21
- EXPORT_DIR.mkdir(parents=True, exist_ok=True)
22
  prs = Presentation()
23
- # タイトル
24
- slide = prs.slides.add_slide(prs.slide_layouts[0])
25
- slide.shapes.title.text = f"提案({company_name}向け)"
26
- slide.placeholders[1].text = "Agent Studio"
27
-
28
- # セクション分割
29
- sections = []
30
- current = {"title": "概要", "bullets": []}
31
- for line in proposal_markdown.splitlines():
32
- if line.startswith("#"):
33
- if current["bullets"]:
34
- sections.append(current)
35
- current = {"title": line.lstrip("# ").strip(), "bullets": []}
36
- elif line.strip():
37
- current["bullets"].append(line.strip())
38
- if current["bullets"]:
39
- sections.append(current)
40
-
41
- for sec in sections:
42
- s = prs.slides.add_slide(prs.slide_layouts[1])
43
- s.shapes.title.text = sec["title"]
44
- body = s.placeholders[1].text_frame
45
- first = True
46
- for b in sec["bullets"][:8]:
47
- if first:
48
- body.text = b
49
- first = False
50
- else:
51
- body.add_paragraph().text = b
52
-
53
- path = EXPORT_DIR / f"proposal_{company_name}.pptx"
54
- prs.save(path)
55
- return str(path)
 
1
+ # modules/exporters.py
2
  from pathlib import Path
3
+ from typing import Optional
4
+
5
  from docx import Document
6
  from pptx import Presentation
7
+ from pptx.util import Pt
8
+
9
+ # utils 側で書き込み可能な場所を自動選択
10
+ from .utils import ensure_dirs, export_dir
11
+
12
 
13
+ def _resolve_outpath(out_path: Optional[str], default_name: str) -> Path:
14
+ ensure_dirs()
15
+ base = export_dir()
16
+ if out_path:
17
+ p = Path(out_path)
18
+ if not p.is_absolute():
19
+ p = base / p
20
+ else:
21
+ p = base / default_name
22
+ p.parent.mkdir(parents=True, exist_ok=True)
23
+ return p
24
+
25
+
26
+ def export_docx(markdown_text: str, out_path: Optional[str] = None) -> str:
27
+ """
28
+ 非依存最小の DOCX エクスポート(Markdown は簡易に段落分割のみ)
29
+ """
30
+ p = _resolve_outpath(out_path, "proposal.docx")
31
 
 
 
32
  doc = Document()
33
+ # シンプルに行単位で追加(見出しの # は太字化のみ)
34
+ for line in (markdown_text or "").splitlines():
35
+ if line.strip().startswith("#"):
36
+ run = doc.add_paragraph().add_run(line.lstrip("# ").strip())
37
+ run.bold = True
38
  else:
39
  doc.add_paragraph(line)
40
+ doc.save(p)
41
+ return str(p)
42
+
43
+
44
+ def export_pptx(markdown_text: str, out_path: Optional[str] = None) -> str:
45
+ """
46
+ 非依存最小の PPTX エクスポート(1行=1スライド、最初の行はタイトル扱い)
47
+ """
48
+ p = _resolve_outpath(out_path, "proposal.pptx")
49
 
 
 
50
  prs = Presentation()
51
+ # 16:9 既定レイアウト(タイトル&本文)を使う
52
+ layout = prs.slide_layouts[1]
53
+
54
+ lines = [ln for ln in (markdown_text or "").splitlines()]
55
+
56
+ # 1枚目: タイトルスライド
57
+ if lines:
58
+ slide0 = prs.slides.add_slide(prs.slide_layouts[0]) # タイトル
59
+ slide0.shapes.title.text = lines[0].lstrip("# ").strip() or "提案ドラフト"
60
+ if len(lines) > 1 and lines[1].strip():
61
+ slide0.placeholders[1].text = lines[1].strip()
62
+
63
+ # 2枚目以降: 残りの行を1行=1スライドで
64
+ for ln in lines[2:] if len(lines) > 2 else []:
65
+ slide = prs.slides.add_slide(layout)
66
+ slide.shapes.title.text = "概要"
67
+ tf = slide.placeholders[1].text_frame
68
+ tf.clear()
69
+ p_para = tf.paragraphs[0]
70
+ p_para.text = ln
71
+ p_para.font.size = Pt(18)
72
+
73
+ prs.save(p)
74
+ return str(p)