Reputation: 5055
I want to make some simple edits on a Word document, eg replace all TEXT
with text
.
I have tried python-docx
, but it doesn't let me save only my changes. Instead it makes a new document with default style and saves it with my content.
Is there a good library(Python or other languages) that supports quick edits on docx?
Upvotes: 3
Views: 2257
Reputation: 73688
Little bit of why you are not able to do simple string replace in docx - a .docx document is a Zip archive in OpenXML format: you have first to uncompress it. Earlier I used to use zip
to unzip the docx & then search for the text, like so -
>>> import zipfile
>>> z = zipfile.ZipFile("yourDocInDocx.docx")
>>> "someText" in z.read("word/document.xml")
True
>>> "random other string" in z.read("word/document.xml")
False
>>> z.close()
But later I found this excellent python library for docx - Python-docx which will solve your problem.
# Import the module
from docx import *
# Open the .docx file
document = opendocx('yourDocInDocx.docx')
# Search returns true if found
search(document,'your search string')
Upvotes: 3