-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdocx_utils.py
More file actions
65 lines (56 loc) · 1.98 KB
/
docx_utils.py
File metadata and controls
65 lines (56 loc) · 1.98 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
from docx.shared import Pt
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
def update_resume_with_skills(skeleton, skills):
"""
Replace the skills section in the resume skeleton with the updated skills.
"""
skeleton["skills"] = skills
return skeleton
# Step 4: Generate Resume in Specific Format
def add_hyperlink(paragraph, text, url):
"""
Add a hyperlink to a paragraph.
"""
# This gets access to the document XML
part = paragraph.part
r_id = part.relate_to(url, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink", is_external=True)
# Create the hyperlink element
hyperlink = OxmlElement('w:hyperlink')
hyperlink.set(qn('r:id'), r_id)
# Create the run element for the text
run = OxmlElement('w:r')
rPr = OxmlElement('w:rPr')
r_style = OxmlElement('w:rStyle')
r_style.set(qn('w:val'), "Hyperlink")
rPr.append(r_style)
run.append(rPr)
# Add the text
t = OxmlElement('w:t')
t.text = text
run.append(t)
hyperlink.append(run)
# Append the hyperlink to the paragraph
paragraph._p.append(hyperlink)
def set_paragraph_spacing(paragraph, before=0, after=0, line_spacing=1):
"""
Adjust the spacing of a paragraph.
"""
paragraph_format = paragraph.paragraph_format
paragraph_format.space_before = Pt(before)
paragraph_format.space_after = Pt(after)
paragraph_format.line_spacing = line_spacing
def add_horizontal_line(paragraph):
"""
Add a horizontal line (border) to a paragraph.
"""
p = paragraph._p # Access the underlying XML paragraph element
p_pr = p.get_or_add_pPr()
p_borders = OxmlElement('w:pBdr')
bottom_border = OxmlElement('w:bottom')
bottom_border.set(qn('w:val'), 'single')
bottom_border.set(qn('w:sz'), '4') # Border size
bottom_border.set(qn('w:space'), '1')
bottom_border.set(qn('w:color'), 'auto') # Black border
p_borders.append(bottom_border)
p_pr.append(p_borders)