Skip to content

Commit 956f5c7

Browse files
authored
[Comp-787-6] IR docx formatting, spacing, and reset numbered lists (#715)
1 parent 5e97b40 commit 956f5c7

File tree

1 file changed

+85
-7
lines changed

1 file changed

+85
-7
lines changed

compliance-api/src/compliance_api/services/inspection_record/inspection_record_doc_generator.py

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,65 @@ def _add_paragraph(container, p_tag, font_size):
130130
run.font.size = font_size
131131

132132

133+
def _get_restarted_list_number_num_id(document):
134+
styles = document.styles
135+
num_id_list_number = -1
136+
137+
# Find numId used by 'List Number' style
138+
for style in styles:
139+
if style.name == "List Number":
140+
num_id_list_number = style._element.pPr.numPr.numId.val
141+
break
142+
143+
if num_id_list_number == -1:
144+
return None
145+
146+
numbering = document.part.numbering_part.numbering_definitions._numbering
147+
ct_num = numbering.num_having_numId(num_id_list_number)
148+
abstract_num_id = ct_num.abstractNumId.val
149+
150+
# Create new numbering instance
151+
new_ct_num = numbering.add_num(abstract_num_id)
152+
new_num_id = new_ct_num.numId
153+
154+
# Force restart at 1
155+
start_override = new_ct_num.add_lvlOverride(0)._add_startOverride()
156+
start_override.val = 1
157+
158+
return new_num_id
159+
160+
133161
def _add_list(container, list_tag, font_size):
134162
style = "List Number" if list_tag.name == "ol" else "List Bullet"
135163

164+
document = container.part.document
165+
restarted_num_id = None
166+
167+
if list_tag.name == "ol":
168+
restarted_num_id = _get_restarted_list_number_num_id(document)
169+
170+
first = True
171+
136172
for li in list_tag.find_all("li", recursive=False):
137173
para = container.add_paragraph(style=style)
138-
text = li.get_text(strip=True)
139-
# Replace 2+ spaces with single space
140-
text = re.sub(r' {2,}', ' ', text)
174+
para.paragraph_format.left_indent = Inches(0.5)
175+
para.paragraph_format.hanging_indent = Inches(0.25)
176+
177+
if list_tag.name == "ol" and first and restarted_num_id is not None:
178+
para_props = para._element.pPr
179+
num_props = para_props._add_numPr()
180+
181+
ilvl = OxmlElement("w:ilvl")
182+
ilvl.set(qn("w:val"), "0")
183+
184+
num_id = OxmlElement("w:numId")
185+
num_id.set(qn("w:val"), str(restarted_num_id))
186+
187+
num_props.append(ilvl)
188+
num_props.append(num_id)
189+
first = False
190+
191+
text = re.sub(r' {2,}', ' ', li.get_text(strip=True))
141192
run = para.add_run(text)
142193
if font_size:
143194
run.font.size = font_size
@@ -366,14 +417,40 @@ def _add_formatted_text_to_table_para(para, p_element):
366417
para.add_run('\n')
367418

368419

369-
def _add_list_to_table_cell(cell, list_element):
420+
def _add_list_to_table_cell(cell, list_element, restart_numbering=True):
370421
"""Add a list (ordered or unordered) to a table cell."""
371422
style = "List Number" if list_element.name == "ol" else "List Bullet"
372423

424+
document = cell.part.document
425+
restarted_num_id = None
426+
427+
if list_element.name == "ol" and restart_numbering:
428+
restarted_num_id = _get_restarted_list_number_num_id(document)
429+
430+
first = True
431+
373432
for li in list_element.find_all('li', recursive=False):
374433
para = cell.add_paragraph(style=style)
375-
text = li.get_text(strip=True)
376-
text = re.sub(r' {2,}', ' ', text)
434+
435+
if (
436+
list_element.name == "ol"
437+
and restart_numbering
438+
and first
439+
and restarted_num_id is not None
440+
):
441+
para_props = para._element.pPr
442+
num_props = para_props._add_numPr()
443+
444+
ilvl = OxmlElement("w:ilvl")
445+
ilvl.set(qn("w:val"), "0")
446+
447+
num_id = OxmlElement("w:numId")
448+
num_id.set(qn("w:val"), str(restarted_num_id))
449+
450+
num_props.append(ilvl)
451+
num_props.append(num_id)
452+
453+
text = re.sub(r' {2,}', ' ', li.get_text(strip=True))
377454
run = para.add_run(text)
378455
run.font.size = Pt(11)
379456

@@ -686,7 +763,7 @@ def generate_inspection_report_docx(preview_data):
686763
logo_para = logo_cell.paragraphs[0]
687764
logo_para.alignment = WD_ALIGN_PARAGRAPH.LEFT
688765
logo_para.paragraph_format.space_before = Pt(0)
689-
logo_para.paragraph_format.space_after = Inches(0.04)
766+
logo_para.paragraph_format.space_after = Inches(0.10)
690767

691768
logo_run = logo_para.add_run()
692769
logo_path = Path(__file__).parent / "assets" / "EAO_Logo.png"
@@ -940,6 +1017,7 @@ def set_header_cell(cell, text):
9401017
if version_date_info and version_date_info.get('preliminary_dates'):
9411018
for date in version_date_info.get('preliminary_dates'):
9421019
para.add_run(f"{date}\n")
1020+
para.runs[-1].text = para.runs[-1].text.rstrip()
9431021
else:
9441022
para.add_run('n/a')
9451023

0 commit comments

Comments
 (0)