Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitccd6b89

Browse files
committed
add docx text replacer tutorial
1 parent7946729 commitccd6b89

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

‎general/docx-file-replacer/README.md

Lines changed: 3 additions & 0 deletions
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`

‎general/docx-file-replacer/doc.docx

36 KB
Binary file not shown.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Import re for regex functions
2+
importre
3+
4+
# Import sys for getting the command line arguments
5+
importsys
6+
7+
# Import docx to work with .docx files.
8+
# Must be installed: pip install python-docx
9+
fromdocximportDocument
10+
11+
# Check if Command Line Arguments are passed.
12+
iflen(sys.argv)<3:
13+
print('Not Enough arguments where supplied')
14+
sys.exit()
15+
16+
# Check if replacers are in a valid schema
17+
forreplaceArginsys.argv[2:]:
18+
iflen(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+
iffile_path.endswith('.docx'):
27+
doc=Document(file_path)
28+
# Loop through replacer arguments
29+
occurences= {}
30+
forreplaceArgsinsys.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+
forparaindoc.paragraphs:
37+
# Loop through runs (style spans)
38+
forruninpara.runs:
39+
# if there is text on this run, replace it
40+
ifrun.text:
41+
# get the replacement text
42+
replaced_text=re.sub(replaceArg[0],replaceArg[1],run.text,999)
43+
ifreplaced_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+
forword,countinoccurences.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')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-docx

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp