From 840227e4ed0c2195231ac2df9023a3bc7a773877 Mon Sep 17 00:00:00 2001 From: Alexander Parrill Date: Wed, 10 Apr 2024 12:35:52 -0400 Subject: [PATCH] Fix exception when converting rich text with paragraph alignments BeautifulSoup4 returns a list for the "class" attribute, but the code was expecting a string. This patch fixes the issue. --- ghostwriter/modules/reportwriter/html_to_docx.py | 2 +- ghostwriter/modules/reportwriter/html_to_pptx.py | 2 +- ghostwriter/reporting/tests/test_rich_text_docx.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ghostwriter/modules/reportwriter/html_to_docx.py b/ghostwriter/modules/reportwriter/html_to_docx.py index 895301900..b83306377 100644 --- a/ghostwriter/modules/reportwriter/html_to_docx.py +++ b/ghostwriter/modules/reportwriter/html_to_docx.py @@ -120,7 +120,7 @@ def tag_p(self, el, *, par=None, **kwargs): # Top level

par = self.doc.add_paragraph(style=self.p_style) - par_classes = set(el.attrs.get("class", "").split()) + par_classes = set(el.attrs.get("class", [])) if "left" in par_classes: par.alignment = WD_ALIGN_PARAGRAPH.LEFT if "center" in par_classes: diff --git a/ghostwriter/modules/reportwriter/html_to_pptx.py b/ghostwriter/modules/reportwriter/html_to_pptx.py index c2bcdb4df..860aeb1b5 100644 --- a/ghostwriter/modules/reportwriter/html_to_pptx.py +++ b/ghostwriter/modules/reportwriter/html_to_pptx.py @@ -85,7 +85,7 @@ def tag_p(self, el, **kwargs): self.text_tracking.new_block() par = self.shape.text_frame.add_paragraph() - par_classes = set(el.attrs.get("class", "").split()) + par_classes = set(el.attrs.get("class", [])) if "left" in par_classes: par.alignment = PP_ALIGN.LEFT if "center" in par_classes: diff --git a/ghostwriter/reporting/tests/test_rich_text_docx.py b/ghostwriter/reporting/tests/test_rich_text_docx.py index 575590d8a..cae1a99db 100644 --- a/ghostwriter/reporting/tests/test_rich_text_docx.py +++ b/ghostwriter/reporting/tests/test_rich_text_docx.py @@ -570,3 +570,16 @@ class RichTextToDocxTests(TestCase): """, ) + + test_paragraph_class = mk_test_docx( + "test_paragraph_class", + """ +

Paragraph with a class

+ """, + """ + + + Paragraph with a class + + """ + )