Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As it seems to be, Docx for Python is not meant to store a full Docx with images, headers, ... , but only contains the inner content of the document. So there's no simple way to do this.</p> <p>Howewer, here is how you could do it:</p> <p>First, have a look at the <a href="https://stackoverflow.com/tags/docx/info">docx tag wiki</a>:</p> <p>It explains how the docx file can be unzipped: Here's how a typical file looks like:</p> <pre><code>+--docProps | + app.xml | \ core.xml + res.log +--word //this folder contains most of the files that control the content of the document | + document.xml //Is the actual content of the document | + endnotes.xml | + fontTable.xml | + footer1.xml //Containst the elements in the footer of the document | + footnotes.xml | +--media //This folder contains all images embedded in the word | | \ image1.jpeg | + settings.xml | + styles.xml | + stylesWithEffects.xml | +--theme | | \ theme1.xml | + webSettings.xml | \--_rels | \ document.xml.rels //this document tells word where the images are situated + [Content_Types].xml \--_rels \ .rels </code></pre> <p>Docx only gets one part of the document, in the method <strong>opendocx</strong></p> <pre><code>def opendocx(file): '''Open a docx file, return a document XML tree''' mydoc = zipfile.ZipFile(file) xmlcontent = mydoc.read('word/document.xml') document = etree.fromstring(xmlcontent) return document </code></pre> <p>It only gets the document.xml file. </p> <p>What I recommend you to do is: </p> <ol> <li>get the content of the document with **opendocx*</li> <li>Replace the document.xml with the <strong>advReplace</strong> method</li> <li>Open the docx as a zip, and replace the document.xml content's by the new xml content.</li> <li>Close and output the zipped file (renaming it to output.docx)</li> </ol> <p>If you have node.js installed, be informed that I have worked on <a href="https://github.com/edi9999/docxgenjs" rel="nofollow noreferrer">DocxGenJS</a> which is templating engine for docx documents, the library is in active development and will be released soon as a node module.</p>
 

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