lpod.document

class lpod.document.odf_document(container)

Bases: object

Abstraction of the ODF document.

add_file(path_or_file)

Insert a file from a path or a fike-like object in the container. Return the full path to reference it in the content.

Arguments:

path_or_file – str or file-like

Return: str

clone()

Return an exact copy of the document.

Return: odf_document

del_part(path)

Mark a part for deletion. The path is relative to the archive, e.g. “Pictures/100000000000032000000258912EB1C3.jpg”

delete_styles()

Remove all style information from content and all styles.

Return: number of deleted styles

get_body()

Return the body element of the content part, where actual content is inserted.

get_formated_meta()
get_formatted_text(rst_mode=False)
get_mimetype()
get_part(path)

Return the bytes of the given part. The path is relative to the archive, e.g. “Pictures/100000000000032000000258912EB1C3.jpg”.

“content”, “meta”, “settings”, “styles” and “manifest” are shortcuts to the real path, e.g. content.xml, and return a dedicated object with its own API.

get_parts()

Return available part names with path inside the archive, e.g. [‘content.xml’, ..., ‘Pictures/100000000000032000000258912EB1C3.jpg’]

get_style(family, name_or_element=None, display_name=None)

Return the style uniquely identified by the name/family pair. If the argument is already a style object, it will return it.

If the name is None, the default style is fetched.

If the name is not the internal name but the name you gave in a desktop application, use display_name instead.

Arguments:

family – ‘paragraph’, ‘text’, ‘graphic’, ‘table’, ‘list’,
‘number’, ‘page-layout’, ‘master-page’

name – unicode or odf_element or None

display_name – unicode

Return: odf_style or None if not found.

get_styled_elements(name=True)

Brute-force to find paragraphs, tables, etc. using the given style name (or all by default).

Arguments:

name – unicode

Return: list

get_styles(family=None, automatic=False)
get_type()

Get the ODF type (also called class) of this document.

Return: ‘chart’, ‘database’, ‘formula’, ‘graphics’,
‘graphics-template’, ‘image’, ‘presentation’, ‘presentation-template’, ‘spreadsheet’, ‘spreadsheet-template’, ‘text’, ‘text-master’, ‘text-template’ or ‘text-web’
insert_style(style, name=None, automatic=False, default=False)

Insert the given style object in the document, as required by the style family and type.

The style is expected to be a common style with a name. In case it was created with no name, the given can be set on the fly.

If automatic is True, the style will be inserted as an automatic style.

If default is True, the style will be inserted as a default style and would replace any existing default style of the same family. Any name or display name would be ignored.

Automatic and default arguments are mutually exclusive.

All styles can’t be used as default styles. Default styles are allowed for the following families: paragraph, text, section, table, table-column, table-row, table-cell, table-page, chart, drawing-page, graphic, presentation, control and ruby.

Arguments:

style – odf_style

name – unicode

automatic – bool

default – bool

Returns : style name – str

merge_styles_from(document)

Copy all the styles of a document into ourself.

Styles with the same type and name will be replaced, so only unique styles will be preserved.

save(target=None, packaging=None, pretty=False, backup=False)

Save the document, at the same place it was opened or at the given target path. Target can also be a file-like object. It can be saved as a Zip file (default) or a flat XML file (unimplemented). XML parts can be pretty printed.

Arguments:

target – str or file-like object

packaging – ‘zip’ or ‘flat’, or for debugging purpose ‘folder’

pretty – bool

backup – boolean

set_part(path, data)

Set the bytes of the given part. The path is relative to the archive, e.g. “Pictures/100000000000032000000258912EB1C3.jpg”.

show_styles(automatic=True, common=True, properties=False)
lpod.document.odf_get_document(path_or_file)

Return an “odf_document” instance of the ODF document stored at the given local path or in the given (open) file-like object.

Examples:

>>> document = odf_get_document('/tmp/document.odt')
>>> stringio = StringIO(...)
>>> document = odf_get_document(stringio)
>>> file = urllib.urlopen('http://example.com/document.odt')
>>> document = odf_get_document(file)
lpod.document.odf_new_document(path_or_file)

Return an “odf_document” instance using the given template or the template found at the given path.

Examples:

>>> document = odf_new_document(template)

>>> path = 'models/invoice.ott'
>>> document = odf_new_document(path)

if “path” is one of ‘text’, ‘spreadsheet’, ‘presentation’, ‘drawing’ or ‘graphics’, then the lpOD default template is used.

Examples:

>>> document = odf_new_document('text')

>>> document = odf_new_document('spreadsheet')

Previous topic

lpod.datatype

Next topic

lpod.draw_page

This Page