Answer the question
In order to leave comments, you need to log in
How to find and change text in paython doc and docx files?
Hello, I am writing a script to replace some words in doc and docx extension files, but for some reason I get an error, please tell me what the problem is
from docx import Document
import os
path = os.chdir(r'C:\\Users\\Vadim\\Desktop\\test')
ld = os.listdir(path)
for doc_file in ld:
print ("reading file")
doc = docx.Document (doc_file)
for par in doc.paragraphs:
par.txt = par.txt.replace("ДВЕМ", "СВЕМ")
for doc_file in ld:
print ("save file")
doc.save(doc_file)
Answer the question
In order to leave comments, you need to log in
You are importing the Document class from the docx module. Then (on line 9) you have the following code:
doc = docx.Document (doc_file)
You call Document from the docx module. But you have not already imported the entire module, but only Document. So change the line to:
doc = Document (doc_file)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question