wordhelpers 0.1.2__py3-none-any.whl → 0.1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
wordhelpers/__init__.py CHANGED
@@ -79,6 +79,45 @@ def replace_placeholder_with_table(
79
79
  delete_paragraph(paragraph)
80
80
 
81
81
 
82
+ def inject_table(
83
+ doc_obj: _Document,
84
+ table: dict | WordTableModel,
85
+ placeholder: str,
86
+ remove_leading_para: bool = True,
87
+ remove_placeholder: bool = True,
88
+ ) -> None:
89
+ """
90
+ Function to relocate a Word table object to immediately follow a given
91
+ reference paragraph identified by the placeholder. Receives as input the
92
+ placeholder string and the Word table object (using docx module).
93
+ After moving the Word table after the placeholder paragraph, delete the
94
+ placeholder paragraph.
95
+ """
96
+
97
+ # Locate the paragraph from the supplied placeholder text
98
+ paragraph: Paragraph = get_para_by_string(doc_obj, placeholder)
99
+ if not paragraph:
100
+ raise ValueError(
101
+ f'WARNING: Could not locate placeholder "{placeholder}"'
102
+ )
103
+
104
+ # Build the word table and add it to the end of the document
105
+ table = (
106
+ build_table(
107
+ doc_obj, table, remove_leading_para=remove_leading_para
108
+ )
109
+ if isinstance(table, dict)
110
+ else build_table(doc_obj, table.model_dump(), remove_leading_para=remove_leading_para)
111
+ )
112
+
113
+ # Move the Word table to a new paragraph immediately after the placeholder paragraph
114
+ paragraph._p.addnext(table._tbl)
115
+
116
+ if remove_placeholder:
117
+ # Delete the placeholder paragraph
118
+ delete_paragraph(paragraph)
119
+
120
+
82
121
  def build_table(
83
122
  docx_obj: _Document | Table, table_dict: dict, remove_leading_para: bool = True
84
123
  ) -> Table:
@@ -1,10 +1,12 @@
1
1
  # Built-In Imports
2
+ from __future__ import annotations
2
3
  import re
3
4
  from enum import Enum
4
5
  from typing import Optional
6
+ import json
5
7
 
6
8
  # Third-Party Imports
7
- from pydantic import BaseModel, field_validator, Field
9
+ from pydantic import BaseModel, field_validator, Field, ConfigDict
8
10
 
9
11
 
10
12
  class AlignmentEnum(str, Enum):
@@ -16,12 +18,14 @@ class AlignmentEnum(str, Enum):
16
18
 
17
19
 
18
20
  class WordParagraphModel(BaseModel):
21
+ model_config = ConfigDict(extra="forbid", validate_assignment=True)
19
22
  style: str | None = None
20
23
  alignment: AlignmentEnum | None = None
21
- text: list[str] = Field(default_factory=list)
24
+ text: list[str] | str = Field(default_factory=list)
22
25
 
23
26
 
24
27
  class WordCellModel(BaseModel):
28
+ model_config = ConfigDict(extra="forbid", validate_assignment=True)
25
29
  width: int | None = None
26
30
  background_color: str | None = None
27
31
  paragraphs: list[WordParagraphModel] = Field(default_factory=list)
@@ -41,11 +45,13 @@ class WordCellModel(BaseModel):
41
45
 
42
46
 
43
47
  class WordRowModel(BaseModel):
48
+ model_config = ConfigDict(extra="forbid", validate_assignment=True)
44
49
  cells: list[WordCellModel | str] = Field(default_factory=list)
45
50
 
46
51
  @field_validator("cells")
47
52
  @classmethod
48
53
  def validate_cells(cls, v):
54
+ # The only valid string value is "merge"
49
55
  if isinstance(v, str):
50
56
  if not v.strip().lower() == "merge":
51
57
  raise ValueError("If a cell is a string, it must be 'merge'")
@@ -53,5 +59,263 @@ class WordRowModel(BaseModel):
53
59
 
54
60
 
55
61
  class WordTableModel(BaseModel):
62
+ model_config = ConfigDict(extra="forbid", validate_assignment=True, frozen=False)
56
63
  style: str | None = None
57
64
  rows: list[WordRowModel] = Field(default_factory=list)
65
+
66
+ def add_row(
67
+ self,
68
+ width: int,
69
+ text: list[str] = [],
70
+ merge_cols: list[int] = [],
71
+ background_color: str | None = None,
72
+ style: str | None = None,
73
+ alignment: AlignmentEnum | None = None,
74
+ ) -> None:
75
+
76
+ # Make sure width is same as existing rows if any
77
+ if self.rows:
78
+ existing_width = len(self.rows[0].cells)
79
+ if width != existing_width:
80
+ raise ValueError(
81
+ f"New row width {width} does not match existing row width {existing_width}"
82
+ )
83
+
84
+ # Make sure text length is less than the row width minus merged columns
85
+ num_merge_cols = len(merge_cols)
86
+ if len(text) > width - num_merge_cols:
87
+ raise ValueError(
88
+ f"Text length {len(text)} exceeded expected length {width - num_merge_cols} based on width and merge_cols"
89
+ )
90
+
91
+ # Make sure merge_cols are valid
92
+ for col in merge_cols:
93
+ if not isinstance(col, int):
94
+ raise ValueError(f"merge_cols must contain integers, got: {type(col)}")
95
+ if col < 0 or col >= width:
96
+ raise ValueError(f"merge_cols contains invalid column index: {col}")
97
+
98
+ # Build the cells
99
+ cells: list = []
100
+ for i in range(width):
101
+ if i in merge_cols:
102
+ # Insert a merge placeholder
103
+ cells.append("merge")
104
+ else:
105
+ # Build a normal cell
106
+ paragraphs: list = []
107
+ if text:
108
+ paragraphs = [
109
+ WordParagraphModel(
110
+ style=style, alignment=alignment, text=[text.pop(0)]
111
+ )
112
+ ]
113
+ cells.append(
114
+ WordCellModel(
115
+ background_color=background_color, paragraphs=paragraphs
116
+ )
117
+ )
118
+ # Re-build the rows
119
+ rows: list = self.rows.copy()
120
+ rows.append(WordRowModel(cells=cells))
121
+ self.rows = rows
122
+
123
+ def add_text_to_row(
124
+ self,
125
+ row_index: int,
126
+ text: list[str],
127
+ style: str | None = None,
128
+ alignment: AlignmentEnum | None = None,
129
+ ) -> None:
130
+ # Validate row_index
131
+ if row_index < 0 or row_index >= len(self.rows):
132
+ raise IndexError(f"Row index {row_index} out of range")
133
+
134
+ # Make sure text length is less than the row width minus merged columns
135
+ num_merge_cols = len(
136
+ [
137
+ cell
138
+ for cell in self.rows[row_index].cells
139
+ if isinstance(cell, str) and cell == "merge"
140
+ ]
141
+ )
142
+ width = len(self.rows[row_index].cells)
143
+ if len(text) > width - num_merge_cols:
144
+ raise ValueError(
145
+ f"Text length {len(text)} exceeded expected length {width - num_merge_cols} based on width and merge_cols"
146
+ )
147
+
148
+ rows = self.rows.copy()
149
+ row = rows[row_index]
150
+ for cell in row.cells:
151
+ if isinstance(cell, str) and cell == "merge":
152
+ continue # Skip merge cells
153
+ if text:
154
+ cell.paragraphs.append(
155
+ WordParagraphModel(
156
+ style=style, alignment=alignment, text=[text.pop(0)]
157
+ )
158
+ )
159
+
160
+ self.rows = rows
161
+
162
+ def add_text_to_cell(
163
+ self,
164
+ row_index: int,
165
+ col_index: int,
166
+ text: str,
167
+ style: str | None = None,
168
+ alignment: AlignmentEnum | None = None,
169
+ ) -> None:
170
+ # Validate row_index and col_index
171
+ if row_index < 0 or row_index >= len(self.rows):
172
+ raise IndexError(f"Row index {row_index} out of range")
173
+ rows = self.rows.copy()
174
+ row = rows[row_index]
175
+ if col_index < 0 or col_index >= len(row.cells):
176
+ raise IndexError(f"Column index {col_index} out of range")
177
+
178
+ cell = row.cells[col_index]
179
+ if isinstance(cell, str) and cell == "merge":
180
+ raise ValueError("Cannot add text to a merged cell")
181
+
182
+ cell.paragraphs.append(
183
+ WordParagraphModel(style=style, alignment=alignment, text=[text])
184
+ )
185
+
186
+ self.rows = rows
187
+
188
+ def style_row(self, row_index: int, text_style: str) -> None:
189
+ # Validate row_index
190
+ if row_index < 0 or row_index >= len(self.rows):
191
+ raise IndexError(f"Row index {row_index} out of range")
192
+
193
+ rows = self.rows.copy()
194
+ row = rows[row_index]
195
+
196
+ for cell in row.cells:
197
+ if isinstance(cell, str) and cell == "merge":
198
+ continue # Skip merge cells
199
+ for paragraph in cell.paragraphs:
200
+ paragraph.style = text_style
201
+
202
+ self.rows = rows
203
+
204
+ def style_cell(self, row_index: int, col_index: int, text_style: str) -> None:
205
+ # Validate row_index and col_index
206
+ if row_index < 0 or row_index >= len(self.rows):
207
+ raise IndexError(f"Row index {row_index} out of range")
208
+ rows = self.rows.copy()
209
+ row = rows[row_index]
210
+ if col_index < 0 or col_index >= len(row.cells):
211
+ raise IndexError(f"Column index {col_index} out of range")
212
+
213
+ cell = row.cells[col_index]
214
+ if isinstance(cell, str) and cell == "merge":
215
+ raise ValueError("Cannot style a merged cell")
216
+
217
+ for paragraph in cell.paragraphs:
218
+ paragraph.style = text_style
219
+
220
+ self.rows = rows
221
+
222
+ def color_row(self, row_index: int, background_color: str) -> None:
223
+ # Validate row_index
224
+ if row_index < 0 or row_index >= len(self.rows):
225
+ raise IndexError(f"Row index {row_index} out of range")
226
+
227
+ rows = self.rows.copy()
228
+ row = rows[row_index]
229
+
230
+ for cell in row.cells:
231
+ if isinstance(cell, str) and cell == "merge":
232
+ continue # Skip merge cells
233
+ cell.background_color = background_color
234
+
235
+ self.rows = rows
236
+
237
+ def color_cell(self, row_index: int, col_index: int, background_color: str) -> None:
238
+ # Validate row_index and col_index
239
+ if row_index < 0 or row_index >= len(self.rows):
240
+ raise IndexError(f"Row index {row_index} out of range")
241
+ rows = self.rows.copy()
242
+ row = rows[row_index]
243
+ if col_index < 0 or col_index >= len(row.cells):
244
+ raise IndexError(f"Column index {col_index} out of range")
245
+
246
+ cell = row.cells[col_index]
247
+ if isinstance(cell, str) and cell == "merge":
248
+ raise ValueError("Cannot color a merged cell")
249
+
250
+ cell.background_color = background_color
251
+
252
+ self.rows = rows
253
+
254
+ def align_row(self, row_index: int, alignment: AlignmentEnum) -> None:
255
+ # Validate row_index
256
+ if row_index < 0 or row_index >= len(self.rows):
257
+ raise IndexError(f"Row index {row_index} out of range")
258
+
259
+ rows = self.rows.copy()
260
+ row = rows[row_index]
261
+
262
+ for cell in row.cells:
263
+ if isinstance(cell, str) and cell == "merge":
264
+ continue # Skip merge cells
265
+ for paragraph in cell.paragraphs:
266
+ paragraph.alignment = alignment
267
+
268
+ self.rows = rows
269
+
270
+ def align_cell(
271
+ self, row_index: int, col_index: int, alignment: AlignmentEnum
272
+ ) -> None:
273
+ # Validate row_index and col_index
274
+ if row_index < 0 or row_index >= len(self.rows):
275
+ raise IndexError(f"Row index {row_index} out of range")
276
+ rows = self.rows.copy()
277
+ row = rows[row_index]
278
+ if col_index < 0 or col_index >= len(row.cells):
279
+ raise IndexError(f"Column index {col_index} out of range")
280
+
281
+ cell = row.cells[col_index]
282
+ if isinstance(cell, str) and cell == "merge":
283
+ raise ValueError("Cannot align a merged cell")
284
+
285
+ for paragraph in cell.paragraphs:
286
+ paragraph.alignment = alignment
287
+
288
+ self.rows = rows
289
+
290
+ def add_table_to_cell(
291
+ self, row_index: int, col_index: int, table: WordTableModel
292
+ ) -> None:
293
+ # Validate row_index and col_index
294
+ if row_index < 0 or row_index >= len(self.rows):
295
+ raise IndexError(f"Row index {row_index} out of range")
296
+ rows = self.rows.copy()
297
+ row = rows[row_index]
298
+ if col_index < 0 or col_index >= len(row.cells):
299
+ raise IndexError(f"Column index {col_index} out of range")
300
+ cell = row.cells[col_index]
301
+
302
+ if isinstance(cell, str) and cell == "merge":
303
+ raise ValueError("Cannot add table to a merged cell")
304
+
305
+ cell.table = table
306
+
307
+ self.rows = rows
308
+
309
+ def delete_row(self, row_index: int) -> None:
310
+ if row_index < 0 or row_index >= len(self.rows):
311
+ raise IndexError(f"Row index {row_index} out of range")
312
+ rows = self.rows.copy()
313
+ del rows[row_index]
314
+ self.rows = rows
315
+
316
+ def pretty_print(self) -> None:
317
+ print(json.dumps(self.model_dump(), indent=4))
318
+
319
+ def write(self, filepath: str) -> None:
320
+ with open(filepath, "w") as f:
321
+ json.dump(self.model_dump(), f, indent=4)
@@ -0,0 +1,268 @@
1
+ Metadata-Version: 2.4
2
+ Name: wordhelpers
3
+ Version: 0.1.4
4
+ Summary: Helpers for working with python-docx
5
+ Author: AJ Cruz
6
+ Author-email: 15045766-a-cruz@users.noreply.gitlab.com
7
+ Requires-Python: >=3.11
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Requires-Dist: pydantic (>=2.12.5,<3.0.0)
14
+ Requires-Dist: python-docx (>=1.2.0,<2.0.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/wordhelpers.svg)](https://img.shields.io/pypi/pyversions/wordhelpers)
18
+ [![PyPI](https://img.shields.io/pypi/v/wordhelpers.svg)](https://pypi.python.org/pypi/wordhelpers)
19
+ [![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
20
+
21
+ # wordhelpers
22
+ Helper functions for [python-docx](https://python-docx.readthedocs.io/en/latest/). I found myself re-learning docx every time I wanted to use it in a project, so this provides and abstraction. You represent Word tables as a properly-formatted Python dictionary or with the provided WordTableModel class and the helper function converts it to a docx table.
23
+
24
+ # Installation
25
+ wordhelpers can be installed via poetry with: ```poetry add wordhelpers```
26
+ or via pip with: ```pip install wordhelpers```
27
+
28
+ # Usage
29
+ For detailed documentation of the python-docx library see [python-docx](https://python-docx.readthedocs.io/en/latest/)
30
+
31
+ 1. Import the python-docx library into your script with:
32
+ ```python
33
+ from docx import Document
34
+ ```
35
+ 1. Import the helpers from this project with:
36
+ ```python
37
+ from wordhelpers import WordTableModel, inject_table
38
+ ```
39
+ 1. Create the docx Word document object with something like:
40
+ ```python
41
+ doc_obj = Document("a_word_template.docx")
42
+ ```
43
+ 1. Add tables to the document object as required (see the next section of this README for info on how to do that)
44
+ 1. When all changes to your document object are complete, write them with the docx `save()` method:
45
+ ```python
46
+ doc_obj.save("output_file.docx")
47
+ ```
48
+ # Adding tables to the document object
49
+ There are two methods available for creating tables for addition to a word document:
50
+ 1. The provided `WordTablesModel` class
51
+ 1. A properly-formatted python dictionary
52
+
53
+ The WordTablesModel class has a number of methods available to help you build the table:
54
+ - ```add_row(width: int, text: list[str] = [], merge_cols: list[int] = [], background_color: str | None = None, style: str | None = None, alignment: AlignmentEnum | None = None)```
55
+ - ```add_text_to_row(row_index: int, text: list[str], style: str | None = None, alignment: AlignmentEnum | None = None)```
56
+ - ```add_text_to_cell(row_index: int, col_index: int, text: str, style: str | None = None, alignment: AlignmentEnum | None = None)```
57
+ - ```style_row(row_index: int, text_style: str)```
58
+ - ```style_cell(row_index: int, col_index: int, text_style: str)```
59
+ - ```color_row(row_index: int, background_color: str)```
60
+ - ```color_cell(row_index: int, col_index: int, background_color: str)```
61
+ - ```align_row(row_index: int, alignment: AlignmentEnum)```
62
+ - ```align_cell(row_index: int, col_index: int, alignment: AlignmentEnum)```
63
+ - ```add_table_to_cell(row_index: int, col_index: int, table: WordTableModel)```
64
+ - ```delete_row(row_index: int)```
65
+ - ```model_dump()```
66
+ - ```pretty_print()```
67
+ - ```write()```
68
+
69
+ If you prefer to create the tables manually via Python dictionary, the dictionary must follow a strict schema that looks something like this:
70
+ ```python
71
+ {
72
+ "style": None,
73
+ "rows": [
74
+ {
75
+ "cells": [
76
+ {
77
+ "width": None,
78
+ "background": None,
79
+ "paragraphs": [{"style":None,"alignment": "center", "text":"Some Text"}],
80
+ "table": {optional child table}
81
+ },
82
+ {
83
+ "merge": None
84
+ },
85
+ ]
86
+ }
87
+ ]
88
+ }
89
+ ```
90
+ The cell **background** attribute is optional. If supplied with a hexidecimal color code, the cell will be shaded that color.
91
+
92
+ The cell **width** attribute is optional. If supplied with a decimal number (inches), it will hard-code that column's width to the supplied value.
93
+
94
+ The cell **table** attribute is optional. It can be used to nest tables within table cells. If "table" is provided, no other keys are required (background, paragraphs, etc).
95
+
96
+ The paragraph **style** attribute is optional. If set to anything besides None it will use the Word style referenced. The style must already exist in the source/template Word document.
97
+
98
+ The paragraph **alignment** attribute is optional. If set to ```"center"``` it will center-align the text within a cell, if set to ```"right"``` it will right-align the text within a cell
99
+
100
+ The **merge** key is optional. If used the cell will be merged with the cell above (from a dictionary view, to the left from a table view). Multiple merges can be used in a row to merge multiple cells.
101
+
102
+ By default a paragraph's **text** property will create a single-line (but wrapped) entry in the cell if the value is a string. If you would like to create a multi-line cell entry, supply the value as a list instead of a string. This will instruct the module to add a line break after each list item.
103
+
104
+ Schema enforcement of the dictionary is done through Pydantic v2 validations.
105
+
106
+ Injection of the table model (either via the class or a raw dictionary) is done via the provided ```inject_table()``` function.
107
+ The function has the following parameters:
108
+ - doc_obj: _Document
109
+ - table: dict
110
+ - placeholder: str
111
+ - remove_leading_para: bool = True
112
+ - remove_placeholder: bool = True
113
+
114
+ Notice the table parameter must be a python dictionary. So if you've created the table via the provided ```WordTableModel``` class you pass it to ```inject_table()``` with: ```my_table.model_dump()```
115
+
116
+ - **<remove_leading_para>** - This is an optional argument. If not set it will default to True. MS Word tables when created automatically have an empty paragraph at the top/beginning of the table cell. This can create unwanted spacing at the top of the table. By default (value set to "True") the paragraph will be deleted. If you want to keep the paragraph (to add text to it), set this to "False".
117
+ - **<remove_placeholder>** - This is an optional argument. You can leave the placeholder (```remove_placeholder=False```) if you need to keep injecting tables below the placeholder before final deletion
118
+
119
+ ### EXAMPLE
120
+ We start with a Microsoft Word template named "source-template.docx" that looks like this:
121
+
122
+ ![Word Template](artwork/word_template.jpg)
123
+
124
+ Our sample Python script looks like this:
125
+ ```python
126
+ from docx import Document
127
+ from wordhelpers import inject_table
128
+
129
+ doc_obj = Document("source-template.docx")
130
+
131
+ my_dictionary = {
132
+ "style": "plain",
133
+ "rows": [
134
+ {
135
+ "cells": [
136
+ {
137
+ "background": "#506279",
138
+ "paragraphs":[{"style": "regularbold", "text": "Header 1:"}]
139
+ },
140
+ {
141
+ "background": "#506279",
142
+ "paragraphs":[{"style": "regularbold", "text": "Header 2:"}]
143
+ },
144
+ {
145
+ "background": "#506279",
146
+ "paragraphs":[{"style": "regularbold", "text": "Header 3:"}]
147
+ }
148
+ ]
149
+ },
150
+ {
151
+ "cells": [
152
+ {
153
+ "background": "#D5DCE4",
154
+ "paragraphs":[{"style": "No Spacing", "text": "Row 1 Data 1:"}]
155
+ },
156
+ {
157
+ "background": "#D5DCE4",
158
+ "paragraphs":[{"style": "No Spacing", "text": "Row 1 Data 2:"}]
159
+ },
160
+ {
161
+ "background": "#D5DCE4",
162
+ "paragraphs":[{"style": "No Spacing", "text": "Row 1 Data 3:"}]
163
+ }
164
+ ]
165
+ },
166
+ {
167
+ "cells": [
168
+ {
169
+ "paragraphs":[{"style": "No Spacing", "text": "Row 2 Data 1:"}]
170
+ },
171
+ {
172
+ "paragraphs":[{"style": "No Spacing", "text": "Row 2 Data 2:"}]
173
+ },
174
+ {
175
+ "paragraphs":[{"style": "No Spacing", "text": "Row 2 Data 3:"}]
176
+ }
177
+ ]
178
+ },
179
+ {
180
+ "cells": [
181
+ {
182
+ "background": "#D5DCE4",
183
+ "paragraphs":[{"style": "No Spacing", "text": "Row 3 Data 1:"}]
184
+ },
185
+ {
186
+ "background": "#D5DCE4",
187
+ "paragraphs":[{"style": "No Spacing", "text": "Row 3 Data 2:"}]
188
+ },
189
+ {
190
+ "background": "#D5DCE4",
191
+ "paragraphs":[{"style": "No Spacing", "text": "Row 3 Data 3:"}]
192
+ }
193
+ ]
194
+ }
195
+ ]
196
+ }
197
+
198
+ inject_table(doc_obj, my_dictionary, "\[py_placeholder1\]")
199
+ doc_obj.save("output_word_doc.docx")
200
+ ```
201
+
202
+ Using the provided ```WordTableModel``` class instead of the raw dictionary, the python code would look like this:
203
+ ```python
204
+ from docx import Document
205
+ from wordhelpers import WordTableModel, inject_table
206
+
207
+ doc_obj = Document("source-template.docx")
208
+
209
+ my_table = WordTableModel()
210
+ my_table.style = "plain"
211
+ my_table.add_row(
212
+ 3,
213
+ text=[
214
+ "Header 1:",
215
+ "Header 2:",
216
+ "Header 3:",
217
+ ],
218
+ background_color="#506279",
219
+ style="regularbold",
220
+ )
221
+ my_table.add_row(
222
+ 3,
223
+ text=[
224
+ "Row 1 Data 1:",
225
+ "Row 1 Data 2:",
226
+ "Row 1 Data 3:",
227
+ ],
228
+ background_color="#D5DCE4",
229
+ style="No Spacing",
230
+ )
231
+ my_table.add_row(
232
+ 3,
233
+ text=[
234
+ "Row 2 Data 1:",
235
+ "Row 2 Data 2:",
236
+ "Row 2 Data 3:",
237
+ ],
238
+ style="No Spacing",
239
+ )
240
+ my_table.add_row(
241
+ 3,
242
+ text=[
243
+ "Row 3 Data 1:",
244
+ "Row 3 Data 2:",
245
+ "Row 3 Data 3:",
246
+ ],
247
+ background_color="#D5DCE4",
248
+ style="No Spacing",
249
+ )
250
+
251
+ inject_table(doc_obj, my_table.model_dump(), "\[py_placeholder1\]")
252
+ doc_obj.save("output_word_doc.docx")
253
+ ```
254
+
255
+ We run the Python script and it produces a new Word document named "output_word_doc.docx" that looks like this:
256
+
257
+ ![Word Template](artwork/word_output.jpg)
258
+
259
+
260
+ The project provides some additional docx functions that may be useful to your project:
261
+ - ```get_para_by_string(doc_obj: _Document, search: str)```: Searches for a keyword in the docx object and returns there paragraph where the keyword is found
262
+ - ```insert_paragraph_after(paragraph: Paragraph, text: str = None, style: str = None)```: Searches for a keyword in the docx object and inserts a new paragraph immediately after it with the supplied text
263
+ - ```delete_paragraph(paragraph: Paragraph)```: Deletes a given paragraph (after you've inserted text after it for example)
264
+
265
+ As well as the following helper functions for building raw dictionary table models:
266
+ - ```insert_text_into_row(cell_text: list)```: Builds a row (dictionary) from a list of text where each list item is a column in the row. Supports "merge"
267
+ - ```insert_text_by_table_coords(table: dict, row: int, col: int, text: str)```: Inserts text into a table dictionary given the row & column numbers.
268
+ - ```generate_table(num_rows: int, num_cols: int, header_row: list, style: str = None)```: Generates a basic table dictionary and populates the headers from a list of text (strings).
@@ -0,0 +1,5 @@
1
+ wordhelpers/__init__.py,sha256=9gR-TRwfToOc3MtkV7gbubbbZs79H6LBJQ-pgdpKV_w,11648
2
+ wordhelpers/pydantic_models.py,sha256=tg2PXpbVWlym3XmF0KGM1LZDxO-oItzhkTTXEQ11x2g,11211
3
+ wordhelpers-0.1.4.dist-info/METADATA,sha256=si1K6RFlC7G4KypZkyVo0KqrWY8NnendWw3cvXIJGl4,11529
4
+ wordhelpers-0.1.4.dist-info/WHEEL,sha256=3ny-bZhpXrU6vSQ1UPG34FoxZBp3lVcvK0LkgUz6VLk,88
5
+ wordhelpers-0.1.4.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.3
2
+ Generator: poetry-core 2.3.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,217 +0,0 @@
1
- Metadata-Version: 2.3
2
- Name: wordhelpers
3
- Version: 0.1.2
4
- Summary: Helpers for working with python-docx
5
- Author: AJ Cruz
6
- Author-email: 15045766-a-cruz@users.noreply.gitlab.com
7
- Requires-Python: >=3.11
8
- Classifier: Programming Language :: Python :: 3
9
- Classifier: Programming Language :: Python :: 3.11
10
- Classifier: Programming Language :: Python :: 3.12
11
- Classifier: Programming Language :: Python :: 3.13
12
- Requires-Dist: pydantic (>=2.12.5,<3.0.0)
13
- Requires-Dist: python-docx (>=1.2.0,<2.0.0)
14
- Description-Content-Type: text/markdown
15
-
16
- [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/wordhelpers.svg)](https://img.shields.io/pypi/pyversions/wordhelpers)
17
- [![PyPI](https://img.shields.io/pypi/v/wordhelpers.svg)](https://pypi.python.org/pypi/wordhelpers)
18
- [![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
19
-
20
- # wordhelpers
21
- =============
22
- Helper functions for [python-docx](https://python-docx.readthedocs.io/en/latest/). I found myself re-learning docx every time I wanted to use it in a project, so this provides and abstraction. You represent Word tables as a properly-formatted Python dictionary and the helper function converts it to a docx table.
23
-
24
- # Installation
25
- wordhelpers can be installed via poetry with: ```poetry add wordhelpers```
26
- or via pip with: ```pip install wordhelpers```
27
-
28
- # Usage
29
- For detailed documentation of the python-docx library see [python-docx](https://python-docx.readthedocs.io/en/latest/)
30
-
31
- 1. Import the python-docx library into your script with:
32
- ```python
33
- from docx import Document
34
- ```
35
- 1. Import the helpers from this project with:
36
- ```python
37
- from wordhelpers import build_table, replace_placeholder_with_table
38
- ```
39
- 1. Create the docx Word document object with something like:
40
- ```python
41
- doc_obj = Document("a_word_template.docx")
42
- ```
43
- 1. Manipulate the document object as required (see the next section of this README for info on how to do that)
44
- 1. When all changes to your document object are complete, write them with:
45
- ```python
46
- doc_obj.save("output_file.docx")
47
- ```
48
- # Manipulating the document object
49
- wordhelpers provides two main functions available to your scripts:
50
- 1. build_table(<doc_obj>, <table_dict>, <remove_leading_para>)
51
- 1. replace_placeholder_with_table(<doc_obj>, <search_string>, <table_obj>)
52
-
53
- ### build_table(<doc_obj>, <table_dict>, <remove_leading_para>)
54
- The purpose of this function is to allow the script author to model Word tables using Python dictionaries. If formatted properly, the module will translate the Python dictionary to the appropriate python-docx syntax and create the Word table object.
55
-
56
- The build_table function has the following arguments:
57
- - **<doc_obj>** - The python-docx Word document object created in step 3 of the "Usage" section above.
58
- - **<table_dict>** - The Word table model (Python dictionary). The expected Python dictionary format to model a Word table is:
59
- ```python
60
- {
61
- "style": None,
62
- "rows": [
63
- {
64
- "cells": [
65
- {
66
- "width": None,
67
- "background": None,
68
- "paragraphs": [{"style":None,"alignment": "center", "text":"Some Text"}],
69
- "table": {optional child table}
70
- },
71
- {
72
- "merge": None
73
- },
74
- ]
75
- }
76
- ]
77
- }
78
- ```
79
- The cell **background** attribute is optional. If supplied with a hexidecimal color code, the cell will be shaded that color.
80
-
81
- The cell **width** attribute is optional. If supplied with a decimal number (inches), it will hard-code that column's width to the supplied value.
82
-
83
- The cell **table** attribute is optional. It can be used to nest tables within table cells. If "table" is provided, no other keys are required (background, paragraphs, etc).
84
-
85
- The paragraph **style** attribute is optional. If set to anything besides None it will use the Word style referenced. The style must already exist in the source/template Word document.
86
-
87
- The paragraph **alignment** attribute is optional. If set to ```"center"``` it will center-align the text within a cell, if set to ```"right"``` it will right-align the text within a cell
88
-
89
- The **merge** key is optional. If used the cell will be merged with the cell above (from a dictionary view, to the left from a table view). Multiple merges can be used in a row to merge multiple cells.
90
-
91
- By default a paragraph's **text** property will create a single-line (but wrapped) entry in the cell if the value is a string. If you would like to create a multi-line cell entry, supply the value as a list instead of a string. This will instruct the module to add a line break after each list item.
92
- - **<remove_leading_para>** - This is an optional argument. If not set it will default to True. MS Word tables when created automatically have an empty paragraph at the top/beginning of the table cell. This can create unwanted spacing at the top of the table. By default (value set to "True") the paragraph will be deleted. If you want to keep the paragraph (to add text to it), set this to "False".
93
-
94
- **IMPORTANT NOTE:** This adds the table object to very end of your Word file. If you want to relocate it, use the provided `replace_placeholder_with_table()` function (see below).
95
-
96
- ### replace_placeholder_with_table(<doc_obj>, <search_string>, <table_obj>)
97
- The purpose of this function is to search a Word file for a given string (the placeholder) and replace the string with a Word table object.
98
-
99
- The replace_placeholder_with_table function has the following arguments:
100
- - **<doc_obj>** - The python-docx Word document object created in step 2 of the "USING PYTHON-DOCX LIBRARY" section above.
101
- - **<search_string>** - The string to search for in the document object (doc_obj)
102
- - **<table_obj>** - The python-docx Word Table object that will replace the <search_string> in the document object (odc_obj)
103
-
104
- It will relocate the table to the placeholder and remove the placeholder.-
105
-
106
- ### EXAMPLE
107
- We start with a Microsoft Word template named "source-template.docx" that looks like this:
108
-
109
- ![Word Template](artwork/word_template.jpg)
110
-
111
- Our sample Python script looks like this:
112
- ```python
113
- from docx import Document
114
- from dcnet_msofficetools.docx_extensions import build_table, replace_placeholder_with_table
115
-
116
- doc_obj = Document("source-template.docx")
117
-
118
- my_dictionary = {
119
- "style": None,
120
- "rows": [
121
- {
122
- "cells": [
123
- {
124
- "paragraphs": [],
125
- "table": {
126
- "style": "plain",
127
- "rows": [
128
- {
129
- "cells": [
130
- {
131
- "background": "#506279",
132
- "paragraphs":[{"style": "regularbold", "text": "Header 1:"}]
133
- },
134
- {
135
- "background": "#506279",
136
- "paragraphs":[{"style": "regularbold", "text": "Header 2:"}]
137
- },
138
- {
139
- "background": "#506279",
140
- "paragraphs":[{"style": "regularbold", "text": "Header 3:"}]
141
- }
142
- ]
143
- },
144
- {
145
- "cells": [
146
- {
147
- "background": "#D5DCE4",
148
- "paragraphs":[{"style": "No Spacing", "text": "Row 1 Data 1:"}]
149
- },
150
- {
151
- "background": "#D5DCE4",
152
- "paragraphs":[{"style": "No Spacing", "text": "Row 1 Data 2:"}]
153
- },
154
- {
155
- "background": "#D5DCE4",
156
- "paragraphs":[{"style": "No Spacing", "text": "Row 1 Data 3:"}]
157
- }
158
- ]
159
- },
160
- {
161
- "cells": [
162
- {
163
- "paragraphs":[{"style": "No Spacing", "text": "Row 2 Data 1:"}]
164
- },
165
- {
166
- "paragraphs":[{"style": "No Spacing", "text": "Row 2 Data 2:"}]
167
- },
168
- {
169
- "paragraphs":[{"style": "No Spacing", "text": "Row 2 Data 3:"}]
170
- }
171
- ]
172
- },
173
- {
174
- "cells": [
175
- {
176
- "background": "#D5DCE4",
177
- "paragraphs":[{"style": "No Spacing", "text": "Row 3 Data 1:"}]
178
- },
179
- {
180
- "background": "#D5DCE4",
181
- "paragraphs":[{"style": "No Spacing", "text": "Row 3 Data 2:"}]
182
- },
183
- {
184
- "background": "#D5DCE4",
185
- "paragraphs":[{"style": "No Spacing", "text": "Row 3 Data 3:"}]
186
- }
187
- ]
188
- }
189
- ]
190
- }
191
- }
192
- ]
193
- }
194
- ]
195
- }
196
-
197
- my_table = build_table(doc_obj, my_dictionary)
198
-
199
- replace_placeholder_with_table(doc_obj, '\[py_placeholder1\]', my_table)
200
-
201
- doc_obj.save("output_word_doc.docx")
202
- ```
203
-
204
- We run the Python script and it produces a new Word document named "output_word_doc.docx" that looks like this:
205
-
206
- ![Word Template](artwork/word_output.jpg)
207
-
208
-
209
- The project provides some additional docx functions that may be useful to your project:
210
- - ```get_para_by_string(doc_obj: _Document, search: str)```: Searches for a keyword in the docx object and returns there paragraph where the keyword is found
211
- - ```insert_paragraph_after(paragraph: Paragraph, text: str = None, style: str = None)```: Searches for a keyword in the docx object and inserts a new paragraph immediately after it with the supplied text
212
- - ```delete_paragraph(paragraph: Paragraph)```: Deletes a given paragraph (after you've inserted text after it for example)
213
-
214
- As well as the following helper functions for the dictionary table models:
215
- - ```insert_text_into_row(cell_text: list)```: Builds a row (dictionary) from a list of text where each list item is a column in the row. Supports "merge"
216
- -```insert_text_by_table_coords(table: dict, row: int, col: int, text: str)```: Inserts text into a table dictionary given the row & column numbers.
217
- - ```generate_table(num_rows: int, num_cols: int, header_row: list, style: str = None)```: Generates a basic table dictionary and populates the headers from a list of text (strings).
@@ -1,5 +0,0 @@
1
- wordhelpers/__init__.py,sha256=CDZRBn1H42Aqjs638FyEYxj-6Jbwk6dGihA8mA2Ij98,10314
2
- wordhelpers/pydantic_models.py,sha256=pb7J6x3p-AmPN1nt7qONArNQztxbfxuUC1dC0DhZ2j0,1643
3
- wordhelpers-0.1.2.dist-info/METADATA,sha256=ZmfUx6jT5OsLN1w6qB-Fl-w3tbQcdKW6lvIF4PHJAJA,11566
4
- wordhelpers-0.1.2.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
5
- wordhelpers-0.1.2.dist-info/RECORD,,