Repository navigation
Expand file tree
/
Copy pathrender_application.py
More file actions
210 lines (162 loc) · 6.34 KB
/
Copy pathrender_application.py
File metadata and controls
210 lines (162 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
"""
render_application.py — deterministic ATS-clean PDF renderer.
Takes the structured content produced by lib/generate-application.js and builds
two PDFs: the tailored résumé and the cover letter. The LLM never controls
layout — this renderer does, so formatting stays consistent and ATS-safe.
Usage: python3 render_application.py <content.json> <output_dir>
Requires: python-docx (pip3 install python-docx)
LibreOffice (soffice on PATH, or /Applications/LibreOffice.app)
ATS rules honoured: single column, standard headings, real bullet lists,
no tables/text-boxes/headers/footers, standard font, dates as MMM YYYY.
"""
import json
import os
import subprocess
import sys
import tempfile
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
FONT = "Calibri"
DARK = RGBColor(0x1a, 0x1a, 0x1a)
def _base(doc):
style = doc.styles["Normal"]
style.font.name = FONT
style.font.size = Pt(10.5)
style.font.color.rgb = DARK
for section in doc.sections:
section.top_margin = section.bottom_margin = Pt(40)
section.left_margin = section.right_margin = Pt(54)
def _heading(doc, text):
p = doc.add_paragraph()
p.space_before = Pt(8)
run = p.add_run(text.upper())
run.bold = True
run.font.size = Pt(11)
p.paragraph_format.space_before = Pt(10)
p.paragraph_format.space_after = Pt(2)
return p
def _line(doc, text, size=10.5, bold=False, align=None, after=2, before=0):
p = doc.add_paragraph()
r = p.add_run(text)
r.bold = bold
r.font.size = Pt(size)
if align is not None:
p.alignment = align
p.paragraph_format.space_after = Pt(after)
p.paragraph_format.space_before = Pt(before)
return p
def _bullet(doc, text):
p = doc.add_paragraph(style="List Bullet")
r = p.add_run(text)
r.font.size = Pt(10.5)
p.paragraph_format.space_after = Pt(2)
return p
def build_resume(content, path):
doc = Document()
_base(doc)
cand = content.get("candidate", {})
r = content.get("resume", {})
# Name + positioning + contact (plain text, never a header)
_line(doc, cand.get("name", ""), size=18, bold=True, align=WD_ALIGN_PARAGRAPH.CENTER, after=1)
if r.get("title"):
_line(doc, r["title"], size=11, bold=False, align=WD_ALIGN_PARAGRAPH.CENTER, after=1)
if cand.get("contact"):
_line(doc, cand["contact"], size=9.5, align=WD_ALIGN_PARAGRAPH.CENTER, after=4)
if r.get("summary"):
_heading(doc, "Professional Summary")
_line(doc, r["summary"])
if r.get("competencies"):
_heading(doc, "Core Competencies")
_line(doc, " · ".join(r["competencies"]))
if r.get("achievements"):
_heading(doc, "Key Achievements")
for a in r["achievements"]:
_bullet(doc, a)
if r.get("experience"):
_heading(doc, "Professional Experience")
for e in r["experience"]:
_line(doc, e.get("titleLine", ""), bold=True, after=0, before=6)
org = e.get("org", "")
dates = e.get("dates", "")
meta = " · ".join([x for x in [org, dates] if x])
if meta:
_line(doc, meta, size=9.5, after=2)
for b in e.get("bullets", []):
_bullet(doc, b)
if r.get("education"):
_heading(doc, "Education")
for ed in r["education"]:
_line(doc, ed)
if r.get("certifications"):
_heading(doc, "Certifications")
for c in r["certifications"]:
_bullet(doc, c)
if r.get("languages"):
_heading(doc, "Languages")
_line(doc, r["languages"])
doc.save(path)
def build_cover(content, path):
doc = Document()
_base(doc)
cand = content.get("candidate", {})
c = content.get("cover", {})
meta = content.get("meta", {})
_line(doc, cand.get("name", ""), size=14, bold=True, after=0)
if cand.get("contact"):
_line(doc, cand["contact"], size=9.5, after=8)
from datetime import date
_line(doc, date.today().strftime("%B %d, %Y"), size=10.5, after=8)
company = meta.get("company", "")
if company:
_line(doc, f"Re: {meta.get('role','')} — {company}", bold=True, after=8)
if c.get("greeting"):
_line(doc, c["greeting"], after=6)
for para in c.get("paragraphs", []):
_line(doc, para, after=8)
if c.get("close"):
_line(doc, c["close"], after=8, before=2)
_line(doc, "Sincerely,", after=2)
_line(doc, c.get("signature", cand.get("name", "")), after=0)
doc.save(path)
def _soffice():
for cand in ("soffice",
"/Applications/LibreOffice.app/Contents/MacOS/soffice",
"/usr/bin/soffice"):
try:
subprocess.run([cand, "--version"], capture_output=True, timeout=15)
return cand
except Exception:
continue
raise RuntimeError("LibreOffice (soffice) not found. Install it, or `brew install --cask libreoffice`.")
def to_pdf(docx_path, out_dir, final_name):
soffice = _soffice()
subprocess.run([soffice, "--headless", "--convert-to", "pdf", "--outdir", out_dir, docx_path],
capture_output=True, timeout=120, check=True)
produced = os.path.join(out_dir, os.path.splitext(os.path.basename(docx_path))[0] + ".pdf")
final = os.path.join(out_dir, final_name)
if produced != final and os.path.exists(produced):
os.replace(produced, final)
return final
def main():
if len(sys.argv) < 3:
print("usage: render_application.py <content.json> <output_dir>", file=sys.stderr)
sys.exit(2)
content_path, out_dir = sys.argv[1], sys.argv[2]
with open(content_path, "r", encoding="utf-8") as fh:
content = json.load(fh)
os.makedirs(out_dir, exist_ok=True)
files = content.get("files", {})
resume_name = files.get("resume", "Resume.pdf")
cover_name = files.get("cover", "Cover.pdf")
with tempfile.TemporaryDirectory() as tmp:
r_docx = os.path.join(tmp, os.path.splitext(resume_name)[0] + ".docx")
c_docx = os.path.join(tmp, os.path.splitext(cover_name)[0] + ".docx")
build_resume(content, r_docx)
build_cover(content, c_docx)
to_pdf(r_docx, out_dir, resume_name)
to_pdf(c_docx, out_dir, cover_name)
print(f"OK {resume_name} {cover_name}")
if __name__ == "__main__":
main()