Skip to content

Commit d1c00ed

Browse files
reindervdw-cmiReinderVosDeWael
authored andcommitted
feat: update style interface
1 parent 6630a34 commit d1c00ed

4 files changed

Lines changed: 26 additions & 69 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cmi_docx"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = ".docx utilities"
55
authors = ["Reinder Vos de Wael <reinder.vosdewael@childmind.org>"]
66
license = "LGPL-2.1"

src/cmi_docx/paragraph.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import itertools
66
import re
77

8-
from docx.enum import text
98
from docx.text import paragraph as docx_paragraph
109
from docx.text import run as docx_run
1110

@@ -141,46 +140,31 @@ def insert_run(self, index: int, text: str, style: styles.RunStyle) -> docx_run.
141140

142141
def format(
143142
self,
144-
*,
145-
bold: bool | None = None,
146-
italics: bool | None = None,
147-
font_size: int | None = None,
148-
font_rgb: tuple[int, int, int] | None = None,
149-
line_spacing: float | None = None,
150-
space_before: float | None = None,
151-
space_after: float | None = None,
152-
alignment: text.WD_PARAGRAPH_ALIGNMENT | None = None,
143+
style: styles.ParagraphStyle,
153144
) -> None:
154145
"""Formats a paragraph in a Word document.
155146
156147
Args:
157-
bold: Whether to bold the paragraph.
158-
italics: Whether to italicize the paragraph.
159-
font_size: The font size of the paragraph.
160-
font_rgb: The font color of the paragraph.
161-
line_spacing: The line spacing of the paragraph.
162-
space_before: The spacing before the paragraph.
163-
space_after: The spacing after the paragraph.
164-
alignment: The alignment of the paragraph.
148+
style: The style to apply to the paragraph.
165149
"""
166-
if line_spacing is not None:
167-
self.paragraph.paragraph_format.line_spacing = line_spacing
150+
if style.line_spacing is not None:
151+
self.paragraph.paragraph_format.line_spacing = style.line_spacing
168152

169-
if alignment is not None:
170-
self.paragraph.alignment = alignment
153+
if style.alignment is not None:
154+
self.paragraph.alignment = style.alignment
171155

172-
if space_before is not None:
173-
self.paragraph.paragraph_format.space_before = space_before
156+
if style.space_before is not None:
157+
self.paragraph.paragraph_format.space_before = style.space_before
174158

175-
if space_after is not None:
176-
self.paragraph.paragraph_format.space_after = space_after
159+
if style.space_after is not None:
160+
self.paragraph.paragraph_format.space_after = style.space_after
177161

178162
for paragraph_run in self.paragraph.runs:
179163
run.ExtendRun(paragraph_run).format(
180164
styles.RunStyle(
181-
bold=bold,
182-
italic=italics,
183-
font_size=font_size,
184-
font_rgb=font_rgb,
165+
bold=style.bold,
166+
italic=style.italic,
167+
font_size=style.font_size,
168+
font_rgb=style.font_rgb,
185169
)
186170
)

src/cmi_docx/styles.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ParagraphStyle:
3636
class TableStyle:
3737
"""Dataclass for table style arguments."""
3838

39+
paragraph: ParagraphStyle | None = None
3940
space_before: float | None = None
4041
space_after: float | None = None
4142
background_rgb: tuple[int, int, int] | None = None

src/cmi_docx/table.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""Extends a python-docx Table cell with additional functionality."""
22

33
from docx import oxml, table
4-
from docx.enum import text
54
from docx.oxml import ns
65

7-
from cmi_docx import paragraph
6+
from cmi_docx import paragraph, styles
87

98

109
class ExtendCell:
@@ -18,49 +17,22 @@ def __init__(self, cell: table._Cell) -> None:
1817
"""
1918
self.cell = cell
2019

21-
def format(
22-
self,
23-
*,
24-
line_spacing: float | None = None,
25-
space_before: float | None = None,
26-
space_after: float | None = None,
27-
bold: bool | None = None,
28-
italics: bool | None = None,
29-
font_size: int | None = None,
30-
font_rgb: tuple[int, int, int] | None = None,
31-
background_rgb: tuple[int, int, int] | None = None,
32-
alignment: text.WD_PARAGRAPH_ALIGNMENT | None = None,
33-
) -> None:
20+
def format(self, style: styles.TableStyle) -> None:
3421
"""Formats a cell in a Word table.
3522
3623
Args:
37-
cell: The cell to format.
38-
line_spacing: The line spacing of the cell.
39-
space_before: The spacing before the cell.
40-
space_after: The spacing after the cell.
41-
bold: Whether to bold the cell.
42-
italics: Whether to italicize the cell.
43-
font_size: The font size of the cell.
44-
font_rgb: The font color of the cell.
45-
background_rgb: The background color of the cell.
46-
alignment: The alignment of the cell.
24+
style: The style to apply to the cell.
4725
"""
48-
for table_paragraph in self.cell.paragraphs:
49-
paragraph.ExtendParagraph(table_paragraph).format(
50-
line_spacing=line_spacing,
51-
bold=bold,
52-
italics=italics,
53-
font_size=font_size,
54-
font_rgb=font_rgb,
55-
alignment=alignment,
56-
space_after=space_after,
57-
space_before=space_before,
58-
)
26+
if style.paragraph is not None:
27+
for table_paragraph in self.cell.paragraphs:
28+
paragraph.ExtendParagraph(table_paragraph).format(style.paragraph)
5929

60-
if background_rgb is not None:
30+
if style.background_rgb is not None:
6131
shading = oxml.parse_xml(
6232
(
63-
r'<w:shd {} w:fill="' + f"{rgb_to_hex(*background_rgb)}" + r'"/>'
33+
r'<w:shd {} w:fill="'
34+
+ f"{rgb_to_hex(*style.background_rgb)}"
35+
+ r'"/>'
6436
).format(
6537
ns.nsdecls("w"),
6638
),

0 commit comments

Comments
 (0)