|
| 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') |
0 commit comments