ODFPY - OpenDocument Python API

1.4.1 · active · verified Thu Apr 09

ODFPY is a Python library and collection of utility programs designed to manipulate OpenDocument Format (ODF) files (e.g., .odt, .ods, .odp). It provides a structured API that adheres rigorously to ODF specifications, focusing on preventing the creation of invalid documents by incorporating grammar checks. The current version is 1.4.1. While new releases are infrequent, it is officially considered actively maintained by its community.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple OpenDocument Text (.odt) file, add a paragraph with text, and save the document using ODFPy. This is a common starting point for generating new ODF files.

from odf.opendocument import OpenDocumentText
from odf.text import P
import os

# Create a new ODT document
textdoc = OpenDocumentText()

# Create a paragraph and add some text
p = P(text="Hello, ODFPy World!")

# Add the paragraph to the document's text body
textdoc.text.addElement(p)

# Save the document to 'helloworld.odt'
# The 'prettyprint=True' argument formats the XML for readability.
try:
    output_filename = os.path.join(os.getcwd(), "helloworld.odt")
    textdoc.save(output_filename, prettyprint=True)
    print(f"Document '{output_filename}' created successfully.")
except Exception as e:
    print(f"Error saving document: {e}")

view raw JSON →