From 5e8d8e1a92836099fd7a17b45b105509d6d1be9b Mon Sep 17 00:00:00 2001 From: Luis Miguel Santos <127312735+luissantosHCIT@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:47:50 -0500 Subject: [PATCH 1/4] Update run.py Allowing copying of another run's font by adding @font.setter definition to font property. --- src/docx/text/run.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/docx/text/run.py b/src/docx/text/run.py index 44c41c0fe..c82d93252 100644 --- a/src/docx/text/run.py +++ b/src/docx/text/run.py @@ -136,6 +136,37 @@ def font(self) -> Font: this run, such as font name and size.""" return Font(self._element) + @font.setter + def font(self, value: Font): + this_font = self.font + this_font.name = value.name + this_font.size = value.size + this_font.strike = value.strike + this_font.subscript = value.subscript + this_font.superscript = value.superscript + this_font.underline = value.underline + this_font.web_hidden = value.web_hidden + this_font.spec_vanish = value.spec_vanish + this_font.snap_to_grid = value.snap_to_grid + this_font.small_caps = value.small_caps + this_font.shadow = value.shadow + this_font.rtl = value.rtl + this_font.outline = value.outline + this_font.no_proof = value.no_proof + this_font.math = value.math + this_font.imprint = value.imprint + this_font.italic = value.italic + this_font.highlight_color = value.highlight_color + this_font.hidden = value.hidden + this_font.emboss = value.emboss + this_font.double_strike = value.double_strike + this_font.cs_italic = value.cs_italic + this_font.cs_bold = value.cs_bold + this_font.complex_script = value.complex_script + this_font.color = value.color + this_font.bold = value.bold + this_font.all_caps = value.all_caps + @property def italic(self) -> bool | None: """Read/write tri-state value. From 954af6708eab9c7a347dc0068efda9da6f519cb8 Mon Sep 17 00:00:00 2001 From: Luis Miguel Santos <127312735+luissantosHCIT@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:49:38 -0500 Subject: [PATCH 2/4] Update font.py Allowing copying of another run's font settings by adding @color.setter definition to color property in Font class. --- src/docx/text/font.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/docx/text/font.py b/src/docx/text/font.py index acd60795b..f5e5ec51b 100644 --- a/src/docx/text/font.py +++ b/src/docx/text/font.py @@ -53,6 +53,12 @@ def color(self): font.""" return ColorFormat(self._element) + @color.setter + def color(self, value: ColorFormat): + this_color = self.color + this_color.theme_color = value.theme_color + this_color.rgb = value.rgb + @property def complex_script(self) -> bool | None: """Read/write tri-state value. From 368e00737fd525e405ba06d7807b7b682945ac6c Mon Sep 17 00:00:00 2001 From: Luis Miguel Santos <127312735+luissantosHCIT@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:50:35 -0500 Subject: [PATCH 3/4] Update run.py Created formatting property based on Steve Canny's suggestion to enable a more comprehensive preservation of the run's formatting. Seems to work well this way. --- src/docx/text/run.py | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/docx/text/run.py b/src/docx/text/run.py index c82d93252..acdfb0a63 100644 --- a/src/docx/text/run.py +++ b/src/docx/text/run.py @@ -1,5 +1,7 @@ """Run-related proxy objects for python-docx, Run in particular.""" +import copy + from __future__ import annotations from typing import IO, TYPE_CHECKING, Iterator, cast @@ -136,36 +138,15 @@ def font(self) -> Font: this run, such as font name and size.""" return Font(self._element) - @font.setter - def font(self, value: Font): - this_font = self.font - this_font.name = value.name - this_font.size = value.size - this_font.strike = value.strike - this_font.subscript = value.subscript - this_font.superscript = value.superscript - this_font.underline = value.underline - this_font.web_hidden = value.web_hidden - this_font.spec_vanish = value.spec_vanish - this_font.snap_to_grid = value.snap_to_grid - this_font.small_caps = value.small_caps - this_font.shadow = value.shadow - this_font.rtl = value.rtl - this_font.outline = value.outline - this_font.no_proof = value.no_proof - this_font.math = value.math - this_font.imprint = value.imprint - this_font.italic = value.italic - this_font.highlight_color = value.highlight_color - this_font.hidden = value.hidden - this_font.emboss = value.emboss - this_font.double_strike = value.double_strike - this_font.cs_italic = value.cs_italic - this_font.cs_bold = value.cs_bold - this_font.complex_script = value.complex_script - this_font.color = value.color - this_font.bold = value.bold - this_font.all_caps = value.all_caps + @property + def formatting(self) -> CT_RPr: + """The |CT_RPr| object providing access to the full range of formatting properties for + this run, such as font name and size.""" + return copy.deepcopy(self._r.get_or_add_rPr()) + + @formatting.setter + def formatting(self, new_rPr: CT_RPr): + self._r.replace(self._r.get_or_add_rPr(), new_rPr) @property def italic(self) -> bool | None: From 04c8f67c8d2fcd616106c0b14dcf27dee5ac1792 Mon Sep 17 00:00:00 2001 From: Luis Miguel Santos <127312735+luissantosHCIT@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:51:45 -0500 Subject: [PATCH 4/4] Update run.py Oops, forgot to move the copy import below _future_ --- src/docx/text/run.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/docx/text/run.py b/src/docx/text/run.py index acdfb0a63..e5b6fa878 100644 --- a/src/docx/text/run.py +++ b/src/docx/text/run.py @@ -1,9 +1,9 @@ """Run-related proxy objects for python-docx, Run in particular.""" -import copy - from __future__ import annotations +import copy + from typing import IO, TYPE_CHECKING, Iterator, cast from docx import types as t