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