Search and replace in .docx using Python

A simple PoC to get you going:

#!/usr/bin/env python2
import zipfile as zf
import StringIO as si
import sys

candidates = ["word/document.xml"]

sio = si.StringIO()
with zf.ZipFile(sio, "w", compression=zf.ZIP_DEFLATED) as zio:
  with zf.ZipFile(sys.argv[1], "r") as za:
    for infile in za.namelist():
      indata = za.read(infile)
      if infile in candidates:
        indata = indata.replace("token", "replacement")
      zio.writestr(infile, indata)

with open("modified_{}".format(sys.argv[1]), "w") as oufile:
  oufile.write(sio.getvalue())

no responses for Search and replace in .docx using Python

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.