Skip to content

Commit

Permalink
fix: Include tables in replace_string function (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrihapponen authored Sep 3, 2024
1 parent 81637fb commit 053b67d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import docxedit

document = Document('path/to/your/document.docx')

# Replace all instances of the word 'Hello' in the document with 'Goodbye'
# Replace all instances of the word 'Hello' in the document with 'Goodbye' (including tables)
docxedit.replace_string(document, old_string='Hello', new_string='Goodbye')

# Replace all instances of the word 'Hello' in the document with 'Goodbye' but only
Expand All @@ -37,6 +37,9 @@ docxedit.replace_string_up_to_paragraph(document, old_string='Hello', new_string
# Remove any line that contains the word 'Hello' along with the next 5 lines after that
docxedit.remove_lines(document, first_line='Hello', number_of_lines=5)

# Add text in table cell (row 1, column 1) in the first table in the document
docxedit.add_text_in_table(document.tables[0], row_num=1, column_num=1, new_string='Hello')

# Save the document
document.save('path/to/your/edited/document.docx')
```
25 changes: 23 additions & 2 deletions docxedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ def show_line(doc: object, current_text: str, show_errors: bool = True):
print(f'Error: An Exception occurred: {e}')


def replace_string(doc: object, old_string: str, new_string: str, show_errors: bool = False):
def replace_string(doc: object, old_string: str, new_string: str,
include_tables: bool = True, show_errors: bool = False):
"""
Replaces an old string (placeholder) with a new string
without changing the formatting of the text.
Args:
doc (Object): The docx document object.
old_string (str): The old string to replace.
new_string (str): The new string to replace the old one with.
include_tables (bool): Whether to include tables or not. Default is True.
show_errors (bool): Whether to show errors or not. Default is False.
Returns:
Success or Error.
Expand All @@ -53,11 +55,30 @@ def replace_string(doc: object, old_string: str, new_string: str, show_errors: b
text = inline[i].text.replace(str(old_string), str(new_string))
inline[i].text = text
string_instances_replaced += 1
print(f'Success: Replaced the string "{old_string}" with "{new_string}"')
print(f'Success: Replaced the string "{old_string}" with "{new_string}" in a paragraph')
else:
if show_errors:
print(f'Error: Could not find the string "{old_string}" in the document')

if include_tables:
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
if old_string in paragraph.text:
inline = paragraph.runs

for i in range(len(inline)):
if old_string in inline[i].text:
text = inline[i].text.replace(str(old_string), str(new_string))
inline[i].text = text
string_instances_replaced += 1
print(f'Success: Replaced the string "{old_string}" '
f'with "{new_string}" in a table')
else:
if show_errors:
print(f'Error: Could not find {old_string} in a table')

print(f'Summary: Replaced {string_instances_replaced} instances of "{old_string}" '
f'with "{new_string}"')

Expand Down

0 comments on commit 053b67d

Please sign in to comment.