Python Forum
Working with Excel and Word, Several Questions Regarding Find and Replace
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with Excel and Word, Several Questions Regarding Find and Replace
#1
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:
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
DeadParrot likes this post

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Working with Excel and Word, Several Questions Regarding Find and Replace - by Brandon_Pickert - Feb-04-2023, 04:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel isnt working properly after python function is started IchNar 0 56 5 hours ago
Last Post: IchNar
  Replace a text/word in docx file using Python Devan 4 3,542 Oct-17-2023, 06:03 PM
Last Post: Devan
  get data from excel and find max/min Timmy94 1 1,127 Jul-27-2022, 08:23 AM
Last Post: Larz60+
  Find and Replace numbers in String giddyhead 2 1,254 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  find some word in text list file and a bit change to them RolanRoll 3 1,565 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  python-docx regex: replace any word in docx text Tmagpy 4 2,267 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  replace exact word marfer 1 6,213 Oct-11-2021, 07:08 PM
Last Post: snippsat
  Working with excel files arsouzaesilva 6 3,216 Sep-17-2021, 06:52 PM
Last Post: arsouzaesilva
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,531 Aug-12-2021, 04:25 PM
Last Post: palladium
  Find and replace in files with regex and Python Melcu54 0 1,861 Jun-03-2021, 09:33 AM
Last Post: Melcu54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020