From 49b19f06a650607c176b6671d39c7307f2d1fccd Mon Sep 17 00:00:00 2001 From: cyy-2024 <1491909552@qq.com> Date: Thu, 24 Oct 2024 09:29:37 +0800 Subject: [PATCH] [fix][html_parser] Improved inline image rendering for proper text-image alignment --- .idea/.gitignore | 8 ++++++ .idea/html2docx.iml | 12 +++++++++ .../inspectionProfiles/profiles_settings.xml | 6 +++++ .idea/misc.xml | 7 ++++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ htmldocx/h2d.py | 25 ++++++++++++++++--- mytest.py | 12 +++++++++ 8 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/html2docx.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 mytest.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/html2docx.iml b/.idea/html2docx.iml new file mode 100644 index 0000000..8b8c395 --- /dev/null +++ b/.idea/html2docx.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3820285 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6f399d9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/htmldocx/h2d.py b/htmldocx/h2d.py index 6a0e113..318396d 100644 --- a/htmldocx/h2d.py +++ b/htmldocx/h2d.py @@ -601,11 +601,30 @@ def run_process(self, html): def add_html_to_document(self, html, document): if not isinstance(html, str): - raise ValueError('First argument needs to be a %s' % str) + raise ValueError('First argument needs to be a string') elif not isinstance(document, docx.document.Document) and not isinstance(document, docx.table._Cell): - raise ValueError('Second argument needs to be a %s' % docx.document.Document) + raise ValueError('Second argument needs to be a Document or a Cell object') + self.set_initial_attrs(document) - self.run_process(html) + soup = BeautifulSoup(html, 'html.parser') + self.paragraph = document.add_paragraph() + for element in soup.descendants: + if isinstance(element, str): + self.run = self.paragraph.add_run(element.strip()) + elif element.name == 'img': + + img_path = element.get('src') + width = element.get('width', 100) + height = element.get('height', 50) + self.run = self.paragraph.add_run() + self.run.add_picture(img_path, width=Inches(float(width) / 100), height=Inches(float(height) / 100)) + else: + continue + + + paragraph_format = self.paragraph.paragraph_format + paragraph_format.line_spacing = 1.0 + paragraph_format.keep_with_next = True def add_html_to_cell(self, html, cell): if not isinstance(cell, docx.table._Cell): diff --git a/mytest.py b/mytest.py new file mode 100644 index 0000000..5434f48 --- /dev/null +++ b/mytest.py @@ -0,0 +1,12 @@ +from docx import Document +from htmldocx import HtmlToDocx + +document = Document() +new_parser = HtmlToDocx() + +html = ''' +

there are some words:测试图片,there are some words in the same line。

+''' + +new_parser.add_html_to_document(html, document) +document.save('test_output.docx')