Skip to main content

docx (OOXML) to html converter

Project description

pydocx
======
[![Build Status](https://travis-ci.org/OpenScienceFramework/pydocx.png?branch=master)](https://travis-ci.org/OpenScienceFramework/pydocx)

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

bold

italics

underline

hyperlinks

headings


#Usage

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

class DocxParser:

@property
def parsed(self):
return self._parsed

@property
def escape(self, text):
return text

@abstractmethod
def linebreak(self):
return ''

@abstractmethod
def paragraph(self, text):
return text

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

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

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

@abstractmethod
def image_handler(self, path):
return path

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

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

@abstractmethod
def bold(self, text):
return text

@abstractmethod
def italics(self, text):
return text

@abstractmethod
def underline(self, text):
return text

@abstractmethod
def tab(self):
return True

@abstractmethod
def ordered_list(self, text):
return text

@abstractmethod
def unordered_list(self, text):
return text

@abstractmethod
def list_element(self, text):
return text

@abstractmethod
def table(self, text):
return text

@abstractmethod
def table_row(self, text):
return text

@abstractmethod
def table_cell(self, text):
return text

@abstractmethod
def page_break(self):
return True

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


Docx2Html inherits DocxParser and implements basic HTML handling. Ex.

class Docx2Html(DocxParser):

def escape(self, text):
return xml.sax.saxutils.quoteattr(text)[1:-1] # Escape '&', '<', and '>' so we
# render the HTML correctly
def linebreak(self, pre=None):
return '<br />' # return a line break

def paragraph(self, text, pre=None):
return '<p>' + text + '</p>' # add paragraph tags


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):
return <p class="my_implementation"> + text + '</p>'



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):

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyDocX-0.1.1.tar.gz (270.5 kB view details)

Uploaded Source

File details

Details for the file PyDocX-0.1.1.tar.gz.

File metadata

  • Download URL: PyDocX-0.1.1.tar.gz
  • Upload date:
  • Size: 270.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyDocX-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2364329a49425a352b00ccb45f070f4803ea9dd12b974f23eea88dcc603f7910
MD5 03bbf8fb93b094356d3f2a04f5a9709a
BLAKE2b-256 1445facc773b68f15a807c920a63f35d6365a378b11c673f11d9c39cb68e2046

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page