Document Management

Global Configuration

ezodf.config

The global Configuration object provides several configuration methods.

classmethod config.set_table_expand_strategy(strategy, maxcount=(32, 32))

Set the global Spreadsheet/Table expand strategy for repeated rows and columns.

Parameters:
  • strategy (str) – 'all' | 'all_but_last' | 'all_less_maxcount' see Open Spreadsheets
  • maxcount (2-tuple) – additional parameter for the strategy 'all_less_maxcount'; maxcount=(10, 20) means: expand all rows with a repetition parameter less 10 and expand all columns with a repetition parameter less 20, all rows/columns with greater repetition parameters were replaced by one row/column.
classmethod config.reset_table_expand_strategy()

Reset the global Spreadsheet/Table expand strategy for repeated rows and columns. Set strategy to 'all_less_maxcount' and maxcount to (32, 32).

Open an existing Document

ezodf.opendoc(filename)
Parameters:filename (str) – a filename or the file-content as bytes
Returns:PackagedDocument or FlatXMLDocument

Open the document filename. Returns an instance of the PackagedDocument class, if the file is a zip-packed document, or an instance of the FlatXMLDocument class, if the document is a single-XML-file document. The document type is determined by the file content.

If you have no access to the filesystem, pass the content of the zip-file (type bytes) as filename parameter. The save() method still works, but no backups will be created.

You can check the document type by the doctype or the mimetype attribute.

Open Spreadsheets

Desktop applications often adding many empty rows and/or empty columns and this library has a very simple cell management strategy - every cell is represented in RAM in a 2-dimensional array - which can fill the whole memory. I have added three different opening strategies for spreadsheets and tables to prevent a memory overflow.

Because loading spreadsheets is an automatic class wrapping process and there is no simple way to pass additional parameters to the opening process (a library design error, sorry), you have to configure the opening strategy by the Global Configuration object.

The three strategies are:

  • expand strategy = 'all' - expand all cells - can cause a memory overflow
  • expand strategy = 'all_but_last' - expand all cells but last row/column, better, but sometimes the penultimate row/column blow up the memory
  • expand strategy = 'all_less_maxcount' - expand all rows/columns with less than maxcount repetitions, rows/columns with maxcount or more repetitions are replaced by one row/column. This is the default strategy, where maxcount=(32, 32), to set the global parameters see Global Configuration object.

Warning

Only the strategy 'all' guarantee the original spreadsheet layout, the other two strategies can break cell references and other strange things can happen, but in most cases they only remove unnecessary appended rows and columns.

example:

import ezodf

# if it is necessary to expand all rows/columns
ezodf.config.set_table_expand_strategy('all')

spreadsheet = ezodf.opendoc('expand_all_cells.ods')

# advice: always reset table expanding strategy
ezodf.config.reset_table_expand_strategy()

Create a new Document

ezodf.newdoc(doctype="odt", filename="", template=None)
Parameters:
  • doctype (str) – document type, three character string like the usual file extensions ('odt' for text, 'ods' for spreadsheets and so on)
  • filename (str) – filename of the document, can also be set by the saveas() method
  • template (str) – filename of a template file or the file-content as bytes, it has to be a zip-packed document and the parameter doctype is ignored, because the template content determines the document type.
Returns:

PackagedDocument

Create a new ODF Document. Returns always an instance of the PackagedDocument class.

If you have no access to the filesystem, pass the content of the zip-file (type bytes) as filename parameter.

Doctype Table

Doctype Mimetype
odt application/vnd.oasis.opendocument.text
ott application/vnd.oasis.opendocument.text-template
odg application/vnd.oasis.opendocument.graphics
otg application/vnd.oasis.opendocument.graphics-template
odp application/vnd.oasis.opendocument.presentation
otp application/vnd.oasis.opendocument.presentation-template
ods application/vnd.oasis.opendocument.spreadsheet
ots application/vnd.oasis.opendocument.spreadsheet-template
odc application/vnd.oasis.opendocument.chart
otc application/vnd.oasis.opendocument.chart-template
odi application/vnd.oasis.opendocument.image
oti application/vnd.oasis.opendocument.image-template
odf application/vnd.oasis.opendocument.formula
otf application/vnd.oasis.opendocument.formula-template
odm application/vnd.oasis.opendocument.text-master
oth application/vnd.oasis.opendocument.text-web

Data Model

I use the lxml package to manage the XML data. You have access to the lxml Elements by the xmlnode attribute in all ODF Content Wrapper classes which bases on the GenericWrapper class.

All document classes have the attributes meta, styles, manifest, content and body and each of them have a xmlnode attribute to the XML representation of the associated XML files manifest.xml, styles.xml, meta.xml and content.xml.

Table Of Contents

Previous topic

Introduction

Next topic

XML Namespace Helper Tools

This Page