Feb-04-2023, 04:41 AM
Looking to loop through Word doc files in a folder and update, or "find and replace" a specific section of the text in the documents. This text is typically in the same spot and same format (ie: "March 1, 2023") but it can change month to month. Another example is the footer with a unique initial (ie: "User - MM").
I have tried using the "os" library and docxtpl library to create variables in the Word document ({{Date}}) but realized having variables is not the route to go because this would be looping through "live" documents, aka, non-template documents. Finding and replacing the mentioned date string might be easy, but doing the same for a footer that has initials may be too unique for a find and replace method. Is there another method besides creating variables or finding and replacing out there? If finding and replacing is the best route, what tips could I utilize to future-proof this automation?
Current code based on variables that will not work:
Windows 10, python version 3.11.1
I have tried using the "os" library and docxtpl library to create variables in the Word document ({{Date}}) but realized having variables is not the route to go because this would be looping through "live" documents, aka, non-template documents. Finding and replacing the mentioned date string might be easy, but doing the same for a footer that has initials may be too unique for a find and replace method. Is there another method besides creating variables or finding and replacing out there? If finding and replacing is the best route, what tips could I utilize to future-proof this automation?
Current code based on variables that will not work:
doc = DocxTemplate(f"C:~filepath~\\{filename}") context = {'Footer': input_footer, 'Effective_Date': input_effective_date} doc.render(context)Screenshot attached.
Windows 10, python version 3.11.1