Skip to content

Commit ac6ed6f

Browse files
committed
refactor(reporting): replace try-except with style checks in footnote generation
Replace try-except blocks with explicit style existence checks when applying Word footnote styles. This makes the code more readable and eliminates exception handling overhead for the common case. Signed-off-by: marc fuller <gogita99@gmail.com>
1 parent 3843862 commit ac6ed6f

File tree

1 file changed

+15
-23
lines changed
  • ghostwriter/modules/reportwriter/richtext

1 file changed

+15
-23
lines changed

ghostwriter/modules/reportwriter/richtext/docx.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -292,23 +292,23 @@ def make_footnote(self, el, *, par=None, **kwargs):
292292
# This is required for Word to properly display the footnote number
293293
footnote_paragraph = new_footnote.add_paragraph()
294294

295-
# Track if either style is missing
296-
missing_footnote_text = False
297-
missing_footnote_ref = False
295+
# Check if required styles exist in the template
296+
has_footnote_text_style = "Footnote Text" in self.doc.styles
297+
has_footnote_ref_style = "Footnote Reference" in self.doc.styles
298298

299-
# Try to apply "Footnote Text" style to the paragraph
300-
try:
299+
# Apply paragraph style if available
300+
if has_footnote_text_style:
301301
footnote_paragraph.style = "Footnote Text"
302-
except KeyError:
303-
missing_footnote_text = True
304302

305-
# Create a run for the footnote reference number
306-
try:
303+
# Create footnote reference run with style if available
304+
if has_footnote_ref_style:
307305
footnote_ref_run = footnote_paragraph.add_run()
308306
footnote_ref_run.style = "Footnote Reference"
309307
footnote_ref_run.font.superscript = True
310-
except KeyError:
311-
missing_footnote_ref = True
308+
footnote_ref_element = OxmlElement("w:footnoteRef")
309+
footnote_ref_run._r.append(footnote_ref_element)
310+
else:
311+
# Fallback: manually create run with XML properties
312312
footnote_ref_run = footnote_paragraph._p.add_r()
313313
run_properties = OxmlElement("w:rPr")
314314
style_element = OxmlElement("w:rStyle")
@@ -318,24 +318,16 @@ def make_footnote(self, el, *, par=None, **kwargs):
318318
vert_align.set(qn("w:val"), "superscript")
319319
run_properties.append(vert_align)
320320
footnote_ref_run.insert(0, run_properties)
321-
322-
# Add the footnote reference mark
323-
footnote_ref_element = OxmlElement("w:footnoteRef")
324-
if hasattr(footnote_ref_run, "_r"):
325-
footnote_ref_run._r.append(footnote_ref_element)
326-
else:
321+
footnote_ref_element = OxmlElement("w:footnoteRef")
327322
footnote_ref_run.append(footnote_ref_element)
328323

329324
# Add a space and the footnote text with "Footnote Text" character style
330325
text_run = footnote_paragraph.add_run(" " + footnote_content)
331-
try:
326+
if has_footnote_text_style:
332327
text_run.style = "Footnote Text"
333-
except KeyError:
334-
missing_footnote_text = True
335-
text_run.font.size = Pt(10)
336328

337-
# If either style is missing, apply default formatting to the paragraph
338-
if missing_footnote_text or missing_footnote_ref:
329+
# Apply fallback formatting if either style is missing
330+
if not has_footnote_text_style or not has_footnote_ref_style:
339331
pf = footnote_paragraph.paragraph_format
340332
pf.line_spacing = 1.0
341333
pf.space_before = 0

0 commit comments

Comments
 (0)