Skip to content

Commit fad9a9e

Browse files
authored
[Comp-787-2] docx formatting changes (#702)
1 parent 2e99532 commit fad9a9e

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ def _add_list(container, list_tag, font_size):
135135

136136
for li in list_tag.find_all("li", recursive=False):
137137
para = container.add_paragraph(style=style)
138-
para.paragraph_format.left_indent = Inches(0.5)
139138
text = li.get_text(strip=True)
140139
# Replace 2+ spaces with single space
141140
text = re.sub(r' {2,}', ' ', text)
@@ -292,6 +291,10 @@ def add_html_table_to_container(container, table_element):
292291
_add_formatted_text_to_table_para(para, element)
293292

294293
elif element.name in ('ul', 'ol'):
294+
# If list is first element, remove the empty default paragraph
295+
if first_element and docx_cell.paragraphs and not docx_cell.paragraphs[0].text.strip():
296+
p = docx_cell.paragraphs[0]._element
297+
p.getparent().remove(p)
295298
# Add list to cell
296299
_add_list_to_table_cell(docx_cell, element)
297300
first_element = False
@@ -419,6 +422,7 @@ def _add_requirement_details_table(doc, req):
419422
para.paragraph_format.space_before = Inches(0.04)
420423
para.paragraph_format.space_after = Inches(0.08)
421424

425+
last_description_is_table = False
422426
for idx, source in enumerate(source_details):
423427
# Add requirement header for first source
424428
if idx == 0:
@@ -497,7 +501,6 @@ def _add_requirement_details_table(doc, req):
497501
section_text += document.get('section_title')
498502
run = section_para.add_run(section_text)
499503
run.bold = True
500-
section_para.add_run('\n')
501504

502505
# Add description
503506
if document.get('description'):
@@ -530,9 +533,16 @@ def _add_requirement_details_table(doc, req):
530533
# Add spacing between multiple sources
531534
if idx < len(source_details) - 1:
532535
para = cell.add_paragraph()
533-
para.add_run('\n')
534536
para = cell.add_paragraph() # New paragraph for next source
535537

538+
# Check if last document description ends with a table
539+
ends_with_table = source.get("requirement_source_description", '').strip().endswith('</table>')
540+
if idx == len(source_details) - 1 and ends_with_table:
541+
last_description_is_table = True
542+
543+
if last_description_is_table:
544+
cell.add_paragraph()
545+
536546
# Inspection Details/Findings
537547
row = req_table.add_row()
538548
cell = row.cells[0].merge(row.cells[1])
@@ -593,6 +603,28 @@ def generate_inspection_report_docx(preview_data):
593603
font.name = 'Calibri'
594604
font.size = Pt(11)
595605

606+
# List styles spacing
607+
list_bullet_style = doc.styles['List Bullet']
608+
list_bullet_style.paragraph_format.space_after = Inches(0.04)
609+
list_bullet_style.paragraph_format.left_indent = Inches(0.5)
610+
611+
# Disable contextual spacing at style level
612+
para_properties = list_bullet_style._element.pPr
613+
if para_properties is not None:
614+
contextual_spacing = para_properties.find(qn('w:contextualSpacing'))
615+
if contextual_spacing is not None:
616+
para_properties.remove(contextual_spacing)
617+
618+
list_number_style = doc.styles['List Number']
619+
list_number_style.paragraph_format.space_after = Inches(0.04)
620+
list_number_style.paragraph_format.left_indent = Inches(0.5)
621+
622+
para_properties = list_number_style._element.pPr
623+
if para_properties is not None:
624+
contextual_spacing = para_properties.find(qn('w:contextualSpacing'))
625+
if contextual_spacing is not None:
626+
para_properties.remove(contextual_spacing)
627+
596628
rpr = style._element.get_or_add_rPr()
597629

598630
# Set East Asia font

0 commit comments

Comments
 (0)