Edit word file . doc in Python

Asked

Viewed 2,796 times

1

I am developing an application in Pythonand Django and need the user to attach a document file .doc. Done this, in this file, the user will have typed some variables, and I need to replace them with information from my system.

Is there any tool to edit documents .docin Python? I found the Python-docx, but it seems that it only serves to create files. I need to open a document and edit the information.

Utilise Python 3.6.

1 answer

1


On the home page of python-docx says:

python-docx is a Python library for Creating and updating Microsoft Word (.docx) files.

In the documentation mentions a builder Document that returns an object from and with that you can work on top of the file. From what I saw, the python-docx has no function search or replace, then it is necessary to iterate on the objects that the Document possesses.

For example:

from docx import Document
document = Document('URL_PARA_SEU_ARQUIVO/Test.docx') 

for paragraph in document.paragraphs:
    if 'teste' in paragraph.text:
        print paragraph.text
        paragraph.text = 'novo texto teste'

In the documentation link I posted mentions these objects that you can work with.

That might help too: How to use python-docx to replace text in a Word Document and save

Browser other questions tagged

You are not signed in. Login or sign up in order to post.