Spaces:
Sleeping
Sleeping
Update modules/exporters.py
Browse files- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 11 |
-
for line in
|
| 12 |
-
if line.startswith("#"):
|
| 13 |
-
doc.
|
|
|
|
| 14 |
else:
|
| 15 |
doc.add_paragraph(line)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
if
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|