Oct-14-2023, 11:31 AM
Hello all,
I have few MS Word documents. I need to replace a particular text in that word document. This particular text can be found in paragraphs or inside tables, header or footer. I mean, where ever that particular text appears,I need to replace them all. I new to python and this is my 1st code,so pls help me.
My code:
Dev.
I have few MS Word documents. I need to replace a particular text in that word document. This particular text can be found in paragraphs or inside tables, header or footer. I mean, where ever that particular text appears,I need to replace them all. I new to python and this is my 1st code,so pls help me.
My code:
from docx import Document
search_text = "WARD"
replace_text = "ORGANIZATION"
with open(r'First.docx', 'r') as file:
data = file.read()
data = data.replace(search_text, replace_text)
with open(r'Second.docx', 'w') as file:
file.write(data)
print("Text replaced")The above program works with .txt files. But it is not working with .docx files, even after using 'from docx import Document'. If i use .docx files, I get the following errors.Error:=========================== RESTART: D:\Work\test.py ===========================
Traceback (most recent call last):
File "D:\Work\test.py", line 14, in <module>
with open(r'Content1.docx', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'Content1.docx'Thanks,Dev.
