Skip to content

Instantly share code, notes, and snippets.

@MattHealy
Created January 3, 2018 07:05
Show Gist options
  • Save MattHealy/4b031c4bb072d9679d0bd7cfd678ff0f to your computer and use it in GitHub Desktop.
Save MattHealy/4b031c4bb072d9679d0bd7cfd678ff0f to your computer and use it in GitHub Desktop.
python-docx example
def merge(r):
if '{{LESSOR}}' in r.text:
r.text = r.text.replace('{{LESSOR}}', 'my name')
if '{{LESSORADDRESS}}' in r.text:
r.text = r.text.replace('{{LESSORADDRESS}}', 'my address')
from docx import Document
document = Document('input.docx')
for t in document.tables:
for r in t.rows:
for c in r.cells:
for p in c.paragraphs:
for r in p.runs:
merge(r)
document.save('output.docx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment