11"""Module for extending python-docx Run objects."""
22
3- from typing import Any
4-
53from docx import shared
64from docx .text import paragraph as docx_paragraph
75
6+ from cmi_docx import styles
7+
88
99class FindRun :
1010 """Data class for maintaing find results in runs.
@@ -40,7 +40,7 @@ def runs(self) -> list[docx_paragraph.Run]:
4040 """Returns the runs containing the text."""
4141 return self .paragraph .runs [self .run_indices [0 ] : self .run_indices [1 ] + 1 ]
4242
43- def replace (self , replace : str , style : dict [ str , Any ] | None = None ) -> None :
43+ def replace (self , replace : str , style : styles . RunStyle | None = None ) -> None :
4444 """Replaces the text in the runs with the replacement text.
4545
4646 Args:
@@ -79,7 +79,7 @@ def _replace_without_style(self, replace: str) -> None:
7979 run .clear ()
8080 self .runs [- 1 ].text = self .runs [- 1 ].text [end :]
8181
82- def _replace_with_style (self , replace : str , style : dict [ str , Any ] ) -> None :
82+ def _replace_with_style (self , replace : str , style : styles . RunStyle ) -> None :
8383 """Replaces the text in the runs with the replacement text and style.
8484
8585 Args:
@@ -95,13 +95,13 @@ def _replace_with_style(self, replace: str, style: dict[str, Any]) -> None:
9595 new_run = self .paragraph ._element ._new_r ()
9696 new_run .text = replace
9797 self .paragraph .runs [self .run_indices [0 ]]._element .addnext (new_run )
98- ExtendRun (self .paragraph .runs [self .run_indices [0 ] + 1 ]).format (** style )
98+ ExtendRun (self .paragraph .runs [self .run_indices [0 ] + 1 ]).format (style )
9999
100100 post_run = self .paragraph ._element ._new_r ()
101101 post_run .text = post
102102 self .paragraph .runs [self .run_indices [0 ] + 1 ]._element .addnext (post_run )
103103 pre_style = ExtendRun (self .paragraph .runs [self .run_indices [0 ]]).get_format ()
104- ExtendRun (self .paragraph .runs [self .run_indices [0 ] + 2 ]).format (** pre_style )
104+ ExtendRun (self .paragraph .runs [self .run_indices [0 ] + 2 ]).format (pre_style )
105105
106106 def __lt__ (self , other : "FindRun" ) -> bool :
107107 """Sorts FindRun in order of appearance in the paragraph.
@@ -135,59 +135,43 @@ def __init__(self, run: docx_paragraph.Run) -> None:
135135 """
136136 self .run = run
137137
138- def format (
139- self ,
140- * ,
141- bold : bool | None = None ,
142- italics : bool | None = None ,
143- underline : bool | None = None ,
144- superscript : bool | None = None ,
145- subscript : bool | None = None ,
146- font_size : int | None = None ,
147- font_rgb : tuple [int , int , int ] | None = None ,
148- ) -> None :
138+ def format (self , style : styles .RunStyle ) -> None :
149139 """Formats a run in a Word document.
150140
151141 Args:
152- bold: Whether to bold the run.
153- italics: Whether to italicize the run.
154- underline: Whether to underline the run.
155- superscript: Whether to superscript the run.
156- subscript: Whether to subscript the run.
157- font_size: The font size of the run.
158- font_rgb: The font color of the run.
142+ style: The style to apply to the run.
159143 """
160- if superscript and subscript :
144+ if style . superscript and style . subscript :
161145 msg = "Cannot have superscript and subscript at the same time."
162146 raise ValueError (msg )
163147
164- if bold is not None :
165- self .run .bold = bold
166- if italics is not None :
167- self .run .italic = italics
168- if underline is not None :
169- self .run .underline = underline
170- if superscript is not None :
171- self .run .font .superscript = superscript
172- if subscript is not None :
173- self .run .font .subscript = subscript
174- if font_size is not None :
175- self .run .font .size = font_size
176- if font_rgb is not None :
177- self .run .font .color .rgb = shared .RGBColor (* font_rgb )
178-
179- def get_format (self ) -> dict [ str , Any ] :
148+ if style . bold is not None :
149+ self .run .bold = style . bold
150+ if style . italic is not None :
151+ self .run .italic = style . italic
152+ if style . underline is not None :
153+ self .run .underline = style . underline
154+ if style . superscript is not None :
155+ self .run .font .superscript = style . superscript
156+ if style . subscript is not None :
157+ self .run .font .subscript = style . subscript
158+ if style . font_size is not None :
159+ self .run .font .size = style . font_size
160+ if style . font_rgb is not None :
161+ self .run .font .color .rgb = shared .RGBColor (* style . font_rgb )
162+
163+ def get_format (self ) -> styles . RunStyle :
180164 """Returns the formatting of the run.
181165
182166 Returns:
183167 The formatting of the run.
184168 """
185- return {
186- " bold" : self .run .bold ,
187- "italics" : self .run .italic ,
188- " underline" : self .run .underline ,
189- " superscript" : self .run .font .superscript ,
190- " subscript" : self .run .font .subscript ,
191- " font_size" : self .run .font .size ,
192- " font_rgb" : self .run .font .color .rgb ,
193- }
169+ return styles . RunStyle (
170+ bold = self .run .bold ,
171+ italic = self .run .italic ,
172+ underline = self .run .underline ,
173+ superscript = self .run .font .superscript ,
174+ subscript = self .run .font .subscript ,
175+ font_size = self .run .font .size ,
176+ font_rgb = self .run .font .color .rgb ,
177+ )
0 commit comments