Welcome to OpenXML Developer Sign in | Join | Help

Create WordprocessingML document using python

Article by Mallika Biswas, Sonata Software Limited

This article explains how to create a WordprocessingML document using python.

Steps to be followed:

1.    Create document.xml.

2.    Create [Content_Types].xml

3.    Create _rels\.rels file i.e the relationship file.

4.    Zip them.

5.    Rename the zip file as .docx

Code explanation:

1.    Import built in module os and minidom.

           import os

           from xml.dom import minidom

2.    To create document.xml, create a document object from the minidom module.

                xml1=minidom.Document()

3.    Add necessary elements and set attributes with them.

             wdoc = xml1.createElement("w:document")

             wdoc.setAttribute("xmlns:w","http://schemas.openxmlformats.org/wordprocessingml/2006/main")

           xml1.appendChild(wdoc)

4.    Similarly, add other elements (w:body,w:p,w:r etc)

5.    Add text value do like this:

              text=xml1.createTextNode("openxml")

6.    Create document.xml file in write mode.

           fp = open(“path\document.xml","w")

7.    Now write the document in this document.xml

               xml1.writexml(fp, "    ", "", "\n", "UTF-8")

8.    Close the file handler

             fp.close()

9.    Create [Content_Types].xml and _rels/.rels file.

10. Creating folder:

            os.mkdir("path where folder has to be created\\_rels")

11. Now after creating all the xml files, zip them.

12. For zipping import built in module  zipfile:

            import zipfile

13. Create the zip file:

           f = zipfile.ZipFile('path of the location \\archive1.zip','w',zipfile.ZIP_DEFLATED)

14. Take the folder name (that is to be zipped) in a variable.

           startdir = "path of the folder to be zipped"

15. For each files and folders of this write them in the zip file:

           for dirpath,dirnames,filenames in os.walk(startdir):

                 for filename in filenames:

                        f.write(os.path.join(dirpath,filename),os.path.join(dirpath,filename).replace(startdir,''))  

        

16. Close the zip file handler:

f.close()

17. Rename the zip file as .docx.

          os.rename("path\\archive1.zip"," path\\test.docx")

18. The .docx file is created in the specified location.

 

A  sample demo which demonstates how to create a wordprocessingML document using python language is attached to this article as a zip file.

Published Sunday, June 08, 2008 11:58 PM by Sheela E N
Filed Under: ,
Attachment(s): CreatingDocxPyhton.zip

Comments

No Comments
Anonymous comments are disabled