Note that there are some explanatory texts on larger screens.

plurals
  1. POLost formatting and image after search and replace using python-docx
    primarykey
    data
    text
    <p>Experts,</p> <p>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. </p> <p>Search &amp; 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.</p> <p>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. </p> <p>Thank you.</p> <p>--- my script is simple one like this ---</p> <pre><code>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') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload