Skip to content

Commit ccd6b89

Browse files
committed
add docx text replacer tutorial
1 parent 7946729 commit ccd6b89

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
110110
- [How to Convert Pandas Dataframes to HTML Tables in Python](https://www.thepythoncode.com/article/convert-pandas-dataframe-to-html-table-python). ([code](general/dataframe-to-html))
111111
- [How to Make a Simple Math Quiz Game in Python](https://www.thepythoncode.com/article/make-a-simple-math-quiz-game-in-python). ([code](general/simple-math-game))
112112
- [How to Make a Network Usage Monitor in Python](https://www.thepythoncode.com/article/make-a-network-usage-monitor-in-python). ([code](general/network-usage))
113+
- [How to Replace Text in Docx Files in Python](https://www.thepythoncode.com/article/replace-text-in-docx-files-using-python). ([code](general/docx-file-replacer))
113114

114115

115116

Diff for: general/docx-file-replacer/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [How to Replace Text in Docx Files in Python](https://www.thepythoncode.com/article/replace-text-in-docx-files-using-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`

Diff for: general/docx-file-replacer/doc.docx

36 KB
Binary file not shown.

Diff for: general/docx-file-replacer/docx_text_replacer.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Import re for regex functions
2+
import re
3+
4+
# Import sys for getting the command line arguments
5+
import sys
6+
7+
# Import docx to work with .docx files.
8+
# Must be installed: pip install python-docx
9+
from docx import Document
10+
11+
# Check if Command Line Arguments are passed.
12+
if len(sys.argv) < 3:
13+
print('Not Enough arguments where supplied')
14+
sys.exit()
15+
16+
# Check if replacers are in a valid schema
17+
for replaceArg in sys.argv[2:]:
18+
if len(replaceArg.split('=')) != 2:
19+
print('Faulty replace argument given')
20+
print('-> ', replaceArg)
21+
sys.exit()
22+
23+
# Store file path from CL Arguments.
24+
file_path = sys.argv[1]
25+
26+
if file_path.endswith('.docx'):
27+
doc = Document(file_path)
28+
# Loop through replacer arguments
29+
occurences = {}
30+
for replaceArgs in sys.argv[2:]:
31+
# split the word=replacedword into a list
32+
replaceArg = replaceArgs.split('=')
33+
# initialize the number of occurences of this word to 0
34+
occurences[replaceArg[0]] = 0
35+
# Loop through paragraphs
36+
for para in doc.paragraphs:
37+
# Loop through runs (style spans)
38+
for run in para.runs:
39+
# if there is text on this run, replace it
40+
if run.text:
41+
# get the replacement text
42+
replaced_text = re.sub(replaceArg[0], replaceArg[1], run.text, 999)
43+
if replaced_text != run.text:
44+
# if the replaced text is not the same as the original
45+
# replace the text and increment the number of occurences
46+
run.text = replaced_text
47+
occurences[replaceArg[0]] += 1
48+
49+
# print the number of occurences of each word
50+
for word, count in occurences.items():
51+
print(f"The word {word} was found and replaced {count} times.")
52+
53+
# make a new file name by adding "_new" to the original file name
54+
new_file_path = file_path.replace(".docx", "_new.docx")
55+
# save the new docx file
56+
doc.save(new_file_path)
57+
else:
58+
print('The file type is invalid, only .docx are supported')

Diff for: general/docx-file-replacer/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-docx

0 commit comments

Comments
 (0)