I'm new to Python:docx and need some help with my script.
- the script should pick up all ".docx" in that folder.
- For each of those ".docx", the Python script replaces certain strings.
- After editing each ".docx", these are saved into a different folder under the original .docx file. I tried a few things but I was unable to complete item 3.
Code below:
# NEW FUNCTION:
import docx
import glob
# This section goes thru the entire order folder (about 1350 docx files in the folder) and picks up .docx to edit then save it into a new folder while keeping the original customer name.
for i in glob.glob(r'C:\Users\LOCAL\Documents\TOPPAN_MOR_BASE_PART\*.docx', recursive=True):
# this section edits the new values into the docx file.
def replacer(p, replace_dict):
inline = p.runs # Specify the list being used
for j in range(0, len(inline)):
# Iterate over the dictionary
for k, v in replace_dict.items():
if k in inline[j].text:
inline[j].text = inline[j].text.replace(k, v)
return p
# Replace Paragraphs & save each docx file with its name intact after content edits.
file_suffix = raw_input(i) # should save file wit original name??
doc = docx.Document(".docx") # Get the file
dict = {'Don X': 'TOPPAN', '5459APSIM':'5499AP', 'Special Mask Build Notes': 'Special Mask Build Notes: Special Mask Build Notes: TOPPAN 4X to 5X'} # Build the dict
for p in doc.paragraphs: # If needed, iter over paragraphs
p = replacer(p, dict) # Call the new replacer function
# This section saves the original *.docx into NewOrder folder. it's not working.
doc.save(r'C:\Users\LOCAL\Documents\TOPPAN_MOR_BASE_PART\NewOrder\SUBSTITUTE'+file_suffix+'docx')
With Python3, please ensure
pip install python-docx.