From 1e8bb8f80b2baab98f8cf17f21cffd3268f48a8a Mon Sep 17 00:00:00 2001 From: robin Date: Wed, 25 Jan 2023 14:49:30 -0800 Subject: [PATCH 1/2] Accounting for pre when adding text run.add_text and assigning to run.text have different outcomes. The latter translates the incoming text to insert docx elements such as tabs and line breaks. ref: https://python-docx.readthedocs.io/en/latest/api/text.html#docx.text.run.Run.text --- html2docx/html2docx.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/html2docx/html2docx.py b/html2docx/html2docx.py index 0b83d1f..70ce413 100644 --- a/html2docx/html2docx.py +++ b/html2docx/html2docx.py @@ -99,7 +99,7 @@ def init_p(self, attrs: List[Tuple[str, Optional[str]]]) -> None: self.padding_left = Pt(style_decl["value"]) def finish_p(self) -> None: - if self.r is not None: + if not self.pre and self.r is not None: self.r.text = self.r.text.rstrip() self._reset() @@ -161,7 +161,10 @@ def add_text(self, data: str) -> None: for attrs in self.attrs: for font_attr, value in attrs: setattr(self.r.font, font_attr, value) - self.r.add_text(data) + if self.pre: + self.r.text = data + else: + self.r.add_text(data) def add_list_style(self, name: str) -> None: self.finish_p() From fe26db7221ae5fc889735715c2e4a8c321994377 Mon Sep 17 00:00:00 2001 From: Robin Chow Date: Tue, 16 Jan 2024 09:26:54 -0800 Subject: [PATCH 2/2] Added tests for `pre` updates --- tests/data/pre-nested-strong.html | 2 ++ tests/data/pre-nested-strong.json | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/data/pre-nested-strong.html create mode 100644 tests/data/pre-nested-strong.json diff --git a/tests/data/pre-nested-strong.html b/tests/data/pre-nested-strong.html new file mode 100644 index 0000000..35db1a3 --- /dev/null +++ b/tests/data/pre-nested-strong.html @@ -0,0 +1,2 @@ +
line1
+line2
\ No newline at end of file diff --git a/tests/data/pre-nested-strong.json b/tests/data/pre-nested-strong.json new file mode 100644 index 0000000..f5b3b5b --- /dev/null +++ b/tests/data/pre-nested-strong.json @@ -0,0 +1,14 @@ +[ + { + "text": "line1\nline2", + "runs": [ + { + "text": "line1\n" + }, + { + "text": "line2", + "bold": true + } + ] + } +]