3

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')
fhd104
  • 61
  • 5

2 Answers2

2

Python-docx only copies over the document.xml file in the original Docx zip. Everything else is discarded and recreated either from a function or from a preexisting template file. This unfortunately includes the document.xml.rels file that is responsible for mapping images.

The oodocx module that I have developed copies over everything from the old Docx and, at least in my experience, plays nicely with images.

Fred the Fantastic
  • 1,295
  • 1
  • 9
  • 11
1

I have answered to a similar question about python-docx. Python docx is not meant to store the docx images and export them away.

Python Docx is not a templating engine for Docx.

Community
  • 1
  • 1
edi9999
  • 19,701
  • 13
  • 88
  • 127