Skip to content

Commit 5a293b3

Browse files
authored
Merge pull request #6 from piglee05022/if6vck-codex/install-required-python-packages
2 parents 43ed903 + 67b6234 commit 5a293b3

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ See [Dockerfile](Dockerfile) for the full details of installed packages.
5656

5757
## Legal document generator module
5858

59-
This repository includes a simple example module located in `legal_module/` that demonstrates how to generate Traditional Chinese legal filings using the `python-docx` package. The module exposes a `create_filing` function that accepts case information and outputs a formatted Word document.
59+
This repository includes a simple example module located in `legal_module/` that demonstrates how to generate Traditional Chinese legal filings using the `python-docx` package. The module exposes a `create_filing` function that accepts case information and outputs a formatted Word document. When `docx2pdf` is available the file can also be exported to PDF. Generated documents use **2.5 cm margins**, **1.5 line spacing**, and include an optional **附件** section for supporting materials.
6060

6161
Example usage:
6262

6363
```bash
6464
python3 -m legal_module.example
6565
```
6666

67-
This requires the optional dependency **python-docx**. Install it with:
67+
This requires the optional dependencies **python-docx** and **docx2pdf** for PDF export. Install them with:
6868

6969
```bash
70-
pip install python-docx
70+
pip install python-docx docx2pdf
7171
```

legal_module/example.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
'evidence': [
1111
{'id': '乙1', 'summary': 'LINE對話紀錄,顯示告知車輛尚未交付'},
1212
{'id': '乙2', 'summary': '川立公司匯款憑證,顯示資金流向'}
13+
],
14+
'attachments': [
15+
{'id': '附件一', 'description': '車輛分期契約影本'},
1316
]
1417
}
1518

1619
if __name__ == '__main__':
17-
create_filing(sample_case, '法律文書_起訴狀.docx')
18-
print('Document generated: 法律文書_起訴狀.docx')
20+
create_filing(sample_case, '法律文書_起訴狀.docx', pdf_path='法律文書_起訴狀.pdf')
21+
print('Documents generated: 法律文書_起訴狀.docx, 法律文書_起訴狀.pdf')

legal_module/filing.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
from typing import Dict
1+
from typing import Any, Dict, Optional
2+
from pathlib import Path
3+
import tempfile
24

35
try:
46
from docx import Document
5-
from docx.shared import Pt
7+
from docx.shared import Pt, Cm
68
except ImportError: # pragma: no cover - docx may not be installed
79
Document = None
810
Pt = None
911

1012
class LegalDocumentGenerator:
1113
"""Generate legal filings in Traditional Chinese."""
1214

13-
def __init__(self, case_info: Dict[str, any]):
15+
def __init__(self, case_info: Dict[str, Any]):
1416
self.case_info = case_info
1517
if Document is None:
1618
raise RuntimeError(
1719
"python-docx is required to generate documents. Please install it via 'pip install python-docx'."
1820
)
1921
self.doc = Document()
20-
style = self.doc.styles['Normal']
22+
style = self.doc.styles["Normal"]
2123
font = style.font
22-
font.name = '標楷體'
24+
font.name = "標楷體"
2325
font.size = Pt(16)
26+
style.paragraph_format.line_spacing = Pt(24) # 1.5 line spacing
27+
28+
for section in self.doc.sections:
29+
section.top_margin = Cm(2.5)
30+
section.bottom_margin = Cm(2.5)
31+
section.left_margin = Cm(2.5)
32+
section.right_margin = Cm(2.5)
2433

2534
def build(self) -> None:
2635
self.doc.add_heading(self.case_info.get('title', '起訴狀'), level=1)
@@ -29,10 +38,25 @@ def build(self) -> None:
2938
self._add_facts()
3039
self._add_laws()
3140
self._add_evidence()
41+
self._add_attachments()
3242

3343
def save(self, filepath: str) -> None:
3444
self.doc.save(filepath)
3545

46+
def save_pdf(self, filepath: str) -> None:
47+
"""Save the generated document as a PDF using docx2pdf if available."""
48+
try:
49+
from docx2pdf import convert
50+
except ImportError as exc: # pragma: no cover - optional dependency
51+
raise RuntimeError(
52+
"docx2pdf is required for PDF export. Install it via 'pip install docx2pdf'."
53+
) from exc
54+
55+
with tempfile.TemporaryDirectory() as tmpdir:
56+
docx_path = Path(tmpdir) / "temp.docx"
57+
self.doc.save(docx_path)
58+
convert(str(docx_path), filepath)
59+
3660
# Internal helpers
3761
def _add_basic_info(self) -> None:
3862
self.doc.add_paragraph(f"案號:{self.case_info.get('case_number', '')}")
@@ -57,10 +81,36 @@ def _add_evidence(self) -> None:
5781
for ev in self.case_info.get('evidence', []):
5882
self.doc.add_paragraph(f"【{ev['id']}{ev['summary']}")
5983

84+
def _add_attachments(self) -> None:
85+
attachments = self.case_info.get("attachments")
86+
if not attachments:
87+
return
88+
self.doc.add_paragraph("伍、附件")
89+
for att in attachments:
90+
desc = att.get("description", "")
91+
att_id = att.get("id", "")
92+
self.doc.add_paragraph(f"【{att_id}{desc}")
93+
94+
95+
def create_filing(
96+
case_info: Dict[str, Any],
97+
output_path: str,
98+
pdf_path: Optional[str] = None,
99+
) -> None:
100+
"""Helper function to quickly generate a filing document.
60101
61-
def create_filing(case_info: Dict[str, any], output_path: str) -> None:
62-
"""Helper function to quickly generate a filing document."""
102+
Parameters
103+
----------
104+
case_info : Dict[str, Any]
105+
Information about the case.
106+
output_path : str
107+
Location to write the DOCX file.
108+
pdf_path : Optional[str]
109+
If provided, also export the document to this PDF path.
110+
"""
63111
generator = LegalDocumentGenerator(case_info)
64112
generator.build()
65113
generator.save(output_path)
114+
if pdf_path:
115+
generator.save_pdf(pdf_path)
66116

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
python-docx
2+
docx2pdf

0 commit comments

Comments
 (0)