skip to navigation
skip to content

Not Logged In

PyDocX 0.3.8

docx (OOXML) to html converter

Latest Version: 0.7.0

.. image::
:align: left

pydocx is a parser that breaks down the elements of a docxfile and converts them
into different markup languages. Right now, HTML is supported. Markdown and LaTex
will be available soon. You can extend any of the available parsers to customize it
to your needs. You can also create your own class that inherits DocxParser
to create your own methods for a markup language not yet supported.

Currently Supported

* tables
* nested tables
* rowspans
* colspans
* lists in tables
* lists
* list styles
* nested lists
* list of tables
* list of pragraphs
* justification
* images
* styles
* bold
* italics
* underline
* hyperlinks
* headings


DocxParser includes abstracts methods that each parser overwrites to satsify its own needs. The abstract methods are as follows:


class DocxParser:

def parsed(self):
return self._parsed

def escape(self, text):
return text

def linebreak(self):
return ''

def paragraph(self, text):
return text

def heading(self, text, heading_level):
return text

def insertion(self, text, author, date):
return text

def hyperlink(self, text, href):
return text

def image_handler(self, path):
return path

def image(self, path, x, y):
return self.image_handler(path)

def deletion(self, text, author, date):
return text

def bold(self, text):
return text

def italics(self, text):
return text

def underline(self, text):
return text

def superscript(self, text):
return text

def subscript(self, text):
return text

def tab(self):
return True

def ordered_list(self, text):
return text

def unordered_list(self, text):
return text

def list_element(self, text):
return text

def table(self, text):
return text
def table_row(self, text):
return text

def table_cell(self, text):
return text

def page_break(self):
return True

def indent(self, text, left='', right='', firstLine=''):
return text

Docx2Html inherits DocxParser and implements basic HTML handling. Ex.


class Docx2Html(DocxParser):

# Escape '&', '<', and '>' so we render the HTML correctly
def escape(self, text):
return xml.sax.saxutils.quoteattr(text)[1:-1]

# return a line break
def linebreak(self, pre=None):
return '

# add paragraph tags
def paragraph(self, text, pre=None):
return '

' + text + '


However, let's say you want to add a specific style to your HTML document. In order to do this, you want to make each paragraph a class of type `my_implementation`. Simply extend docx2Html and add what you need.


class My_Implementation_of_Docx2Html(Docx2Html):

def paragraph(self, text, pre = None):

+ text + '


OR, let's say FOO is your new favorite markup language. Simply customize your own new parser, overwritting the abstract methods of DocxParser


class Docx2Foo(DocxParser):

# because linebreaks in are denoted by '!!!!!!!!!!!!' with the FOO markup langauge :)
def linebreak(self):
return '!!!!!!!!!!!!'

Custom Pre-Processor

When creating your own Parser (as described above) you can now add in your own custom Pre Processor. To do so you will need to set the `pre_processor` field on the custom parser, like so:


class Docx2Foo(DocxParser):
pre_processor_class = FooPrePorcessor

The `FooPrePorcessor` will need a few things to get you going:


class FooPrePorcessor(PydocxPrePorcessor):
def perform_pre_processing(self, root, *args, **kwargs):
super(FooPrePorcessor, self).perform_pre_processing(root, *args, **kwargs)

def _set_foo(self, root):

If you want `_set_foo` to be called you must add it to `perform_pre_processing` which is called in the base parser for pydocx.

Everything done during pre-processing is executed prior to `parse` being called for the first time.


The base parser `Docx2Html` relies on certain css class being set for certain behaviour to occur. Currently these include:

* class `pydocx-insert` -> Turns the text green.
* class `pydocx-delete` -> Turns the text red and draws a line through the text.
* class `pydocx-center` -> Aligns the text to the center.
* class `pydocx-right` -> Aligns the text to the right.
* class `pydocx-left` -> Aligns the text to the left.
* class `pydocx-comment` -> Turns the text blue.
* class `pydocx-underline` -> Underlines the text.
* class `pydocx-caps` -> Makes all text uppercase.
* class `pydocx-small-caps` -> Makes all text uppercase, however truly lowercase letters will be small than their uppercase counterparts.
* class `pydocx-strike` -> Strike a line through.
* class `pydocx-hidden` -> Hide the text.


Right now there is only one custom exception (`MalformedDocxException`). It is raised if either the `xml` or `zipfile` libraries raise an exception.

Optional Arguments

You can pass in `convert_root_level_upper_roman=True` to the parser and it will convert all root level upper roman lists to headings instead.

* 0.3.8
* If zipfile fails to open the passed in file, we are now raising a
`MalformedDocxException` instead of a `BadZipFIle`.
* 0.3.7
* Some inline tags (most notably the underline tag) could have a `val` of
`none` and that would signify that the style is disabled. A `val` of
`none` is now correctly handled.
* 0.3.6
* It is possible for a docx file to not contain a `numbering.xml` file but
still try to use lists. Now if this happens all lists get converted to
* 0.3.5
* Not all docx files contain a `styles.xml` file. We are no longer assuming
they do.
* 0.3.4
* It is possible for `w:t` tags to have `text` set to `None`. This no
longer causes an error when escaping that text.
* 0.3.3
* In the event that `cElementTree` has a problem parsing the document, a
`MalformedDocxException` is raised instead of a `SyntaxError`
* 0.3.2
* We were not taking into account that vertical merges should have a
continue attribute, but sometimes they do not, and in those cases word
assumes the continue attribute. We updated the parser to handle the
cases in which the continue attribute is not there.
* We now correctly handle documents with unicode character in the
* In rare cases, some text would be output with a style when it should not
have been. This issue has been fixed.
* 0.3.1
* Added support for several more OOXML tags including:
* caps
* smallCaps
* strike
* dstrike
* vanish
* webHidden
More details in the README.
* 0.3.0
* We switched from using stock *xml.etree.ElementTree* to using
*xml.etree.cElementTree*. This has resulted in a fairly significant speed
increase for python 2.6
* It is now possible to create your own pre processor to do additional pre
* Superscripts and subscripts are now extracted correctly.
* 0.2.1
* Added a changelog
* Added the version in pydocx.__init__
* Fixed an issue with duplicating content if there was indentation or
justification on a p element that had multiple t tags.  
File Type Py Version Uploaded on Size
PyDocX-0.3.8.tar.gz (md5) Source 2014-05-21 355KB
  • Downloads (All Versions):
  • 694 downloads in the last day
  • 4505 downloads in the last week
  • 7007 downloads in the last month