From 0832798636a2a02928c64c2706f0f0b34fcf128e Mon Sep 17 00:00:00 2001 From: johnjor Date: Wed, 11 Oct 2023 15:36:28 -0400 Subject: [PATCH] Fix for KeyError 'src' when encountering an img tag without a src attribute. Instead, it adds a paragraph with a placeholder, which is consistent with the other failure modes of this method. Added a test for this case. --- htmldocx/h2d.py | 6 +++++- tests/test.py | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/htmldocx/h2d.py b/htmldocx/h2d.py index 6a0e113..c6ad303 100644 --- a/htmldocx/h2d.py +++ b/htmldocx/h2d.py @@ -295,7 +295,11 @@ def handle_img(self, current_attrs): self.skip = True self.skip_tag = 'img' return - src = current_attrs['src'] + src = current_attrs.get('src') + if not src: + self.doc.add_paragraph("") + return + # fetch image src_is_url = is_url(src) if src_is_url: diff --git a/tests/test.py b/tests/test.py index c31cd08..9af248b 100644 --- a/tests/test.py +++ b/tests/test.py @@ -205,6 +205,13 @@ def test_handling_hr(self): ) self.parser.add_html_to_document("

paragraph


paragraph

", self.document) + def test_image_no_src(self): + self.document.add_heading( + 'Test: Handling img without src', + level=1 + ) + self.parser.add_html_to_document("", self.document) + if __name__ == '__main__': unittest.main()