D
D
DDD2021-05-21 00:13:28
Python
DDD, 2021-05-21 00:13:28

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)


The mistake itself
60a6d0e18a7fc072144808.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uprj, 2021-05-24
@uprj

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 question

Ask a Question

731 491 924 answers to any question