Experts,
I have a template docx report, which has image and standard formatting inside it. What I did using docx, was just to search some tags, and replace it using the value from a config file.
Search & replace was working as expected, but the output file lost all the image, and the formatting. Do you know what went wrong? All I did was just modifying the example-makedocument.py, and replace it to use with my docx file.
I've searched the discussion on python.docx librelist, and their page on github, there were a lot of questions like this, but remained unanswered.
Thank you.
--- my script is simple one like this ---
from docx import *
from ConfigParser import SafeConfigParser
filename = "template.docx"
document = opendocx(filename)
relationships = relationshiplist()
body = document.xpath('/w:document/w:body',namespaces=nsprefixes)[0]
####### get config file
parser = SafeConfigParser()
parser.read('../TESTING1-config.txt')
######## Search and replace
print 'Searching for something in a paragraph ...',
if search(body, ''):
print 'found it!'
else:
print 'nope.'
print 'Replacing ...',
body = advReplace(body, '', parser.get('ASD', 'ASD'))
print 'done.'
####### #Create our properties, contenttypes, and other support files
title = 'Python docx demo'
subject = 'A practical example of making docx from Python'
creator = 'Mike MacCana'
keywords = ['python', 'Office Open XML', 'Word']
coreprops = coreproperties(title=title, subject=subject, creator=creator,keywords=keywords)
appprops = appproperties()
contenttypes = contenttypes()
websettings = websettings()
wordrelationships = wordrelationships(relationships)
savedocx(document, coreprops, appprops, contenttypes, websettings, wordrelationships, 'Welcome to the Python docx module.docx')