Skip to content
@deckyard/docs

ODT

readOdt reads an OpenDocument text file (.odt, as LibreOfficeâ„¢ and other ODF producers write it) into a Document; writeOdt writes one as an ODF 1.3 package.

jennifer
import "@deckyard/docs/" as docs;
use fs;
use io;

def d as docs.Document init docs.readOdt(fs.readBytes("letter.odt"));
io.printf("%s\n", docs.plainText($d));
fs.writeBytes("letter.docx", docs.writeDocx($d));
fs.writeBytes("copy.odt", docs.writeOdt($d));

Only the packaged .odt is read. A flat .fodt (one XML file) is refused.

Reading

OpenDocumentDocument
text:p, text:hparagraph; text:h is a heading at its text:outline-level (7 to 10 as 6)
paragraph stylerole (below); fo:text-align as align, fo:break-before / fo:break-after="page" as a "pagebreak" block
text:span, nestedRunStyle from its text style and that style's parents, over the paragraph's direct formatting
fo:font-weight fo:font-style style:text-underline-style style:text-line-through-style style:text-position fo:color fo:font-size fo:background-color style:font-name fo:font-familybold italic underline strike superscript/subscript color size highlight font
a fixed-pitch font (style:font-pitch="fixed", generic family modern) or a monospace family (Courier, Consolas, * Mono, ...)code; font kept unless it is Liberation Mono
character data, text:s, text:tab, text:line-breaktext with ODF's white-space rules (runs collapse, a leading space is dropped), spaces, tab, line break
text:a, draw:alink (xlink:href, #name for a bookmark)
text:note (footnote or endnote)"note" inline; its text:note-body in notes, in document order
office:annotation"comment" inline; author, date and text in comments
draw:frame with draw:image"image" inline and a Media; size from svg:width / svg:height at 96 dpi, alt text from svg:desc, else svg:title
draw:frame with draw:text-boxthe box's text in place
text:list, text:list-item, text:list-headerlistId and listLevel; a nested list continues its list one level deeper
text:list-styleListDef from its level 1: text:list-level-style-number with style:num-format 1 a A i I and text:start-value, else bullets
table:tabletable; table:number-columns-spanned / number-rows-spanned as spans, covered cells left out, rows in a leading table:table-header-rows as headerRows, row groups and repeats expanded, tables in cells
text:tracked-changestext between text:change-start and text:change-end of an insertion: change "insert"; a text:change of a deletion: a "delete" inline holding the deleted paragraphs, joined by \n
text:section, index bodies and titles, fields (text:page-number, ...)their content
styles.xml master page Standard (else the first)header / footer from style:header / style:footer, page from its page layout
meta.xmlmeta; several meta:keyword joined by ", ", meta:initial-creator else dc:creator as author

A text:list starts a new list unless text:continue-list names an earlier one (by xml:id) or text:continue-numbering="true" continues the last list of its style; that list's listId is reused.

Paragraph roles

The role comes from the first style up the style:parent-style-name chain whose name (_20_ as a space) or display name is one of these, case-insensitive:

Style nameRole
Heading 1 to Heading 10"heading", its level (7 to 10 as 6)
Title, Subtitle"title", "subtitle"
Quotations, Quote"quote"
Preformatted Text"code"
Caption (and Figure, Table, ... below it)"caption"
List Heading / List Contents"term" / "definition"
Horizontal Line"rule"
Standard, Text body, Table Contents, Table Heading, Footnote, Header, Footer, ...body text

The text and paragraph properties of the styles below that one are the paragraph's direct formatting; the role style's own formatting (a bold, centred Title) is not. A code paragraph's line breaks and tabs become \n and \t in its text, as fromDoctree writes a code block. An empty body paragraph whose style breaks the page before it is read as the page break alone.

Not read

WhatWhy
Pictures other than PNG, JPEG and GIF (SVG, EMF, WMF), linked picturesMedia holds those three; the inline is left out
Shapes, charts, formulas, embedded objectsno place in the model
Notes and comments in the header or footerthe header and footer are read for their blocks only
style:header-left, style:header-first, other master pagesone header and footer per document
Paragraph and table formatting beyond role, alignment and page breaksnot modelled
Bookmarks, reference marks, index marks, soft page breaksmarkers, not content
A list item's own text:start-valuea list's start is its style's

Strictness

InputError
not a ZIP, a flat .fodt, no mimetype or one other than application/vnd.oasis.opendocument.text, no content.xml, no office:text"docs"
content.xml without the standard text and office prefixes, content after its root"docs"
malformed XML anywhere in a part (checked by core xml first): an element without its closing tag, a stray close tag, an unquoted attribute, a DOCTYPE, an undeclared prefix, an unknown entity, text that is not UTF-8, a character XML forbids"docs"
bare text where only elements belong"docs"
a span of 0 or less, a font size of 0 or less or past 1638 pt, a page side outside 1 to 288,000 twips, margins that leave no room, anything checkDocument refuses"docs"
a bad text:c, text:outline-level, text:start-value, span or repeat count, a bad length or fo:text-align"docs"
a frame referring to a member the package does not hold"docs"
more than MAX_BLOCKS blocks, MAX_CELLS cells (repeats multiplied, counted before the rows are built) or MAX_TEXT characters (spaces and tabs expanded) in the whole document; a row of more than 16,384 cells, a text:s of more than 10,000 spaces, a span past MAX_SPAN"limit"
spans, lists, sections or tables nested past MAX_NESTING, a style chain longer than that (or a cycle)"limit"

A paragraph or text style name that names no style is read as plain body text. Comments and CDATA inside a paragraph are handled (CDATA is text). Normalised rather than refused: negative margins (absolute value), a date that is not ISO 8601 (""), a note inside a note (its text is read in place), a picture side outside 1 to 100,000 px (clamped), a ; in a font name (dropped), a half-point size (rounded up to 2), a row with no cell under no span (one empty cell), a row span past the last row (cut).

Writing

writeOdt writes mimetype (stored, first), META-INF/manifest.xml, content.xml, styles.xml, meta.xml and the pictures under Pictures/.

DocumentOpenDocument
paragraph rolesnamed styles Text_20_body, Heading_20_1 to _6 (as text:h), Title, Subtitle, Quotations, Preformatted_20_Text, Caption, List_20_Heading, List_20_Contents, Horizontal_20_Line (a bottom border)
alignan automatic paragraph style P1, ... over the role style
each distinct RunStylean automatic text style T1, ...; code without a font in Liberation Mono
runs of spaces, \t, \ntext:s, text:tab, text:line-break
a lista list style L1, ... per ListDef; items nested by listLevel; a list interrupted by other blocks continues with text:continue-list
a tabletable:table with its header rows in table:table-header-rows, spans with covered cells; header cells in Table_20_Heading, others in Table_20_Contents
a picturedraw:frame anchored as a character, its size in cm, svg:desc as alt text
a note referencethe text:note with its body, in place
a page breakan empty paragraph whose style breaks the page before it
header / footer, pagethe Standard master page and its page layout
metameta.xml

The same document always gives the same bytes. Comments are not written; deleted tracked text is left out and inserted text written as plain text.

readOdt(writeOdt(d)) gives d back, except:

ModelRead back as
\n and \t in a text inline (outside a code paragraph)"break" and "tab" inlines
neighbouring text inlines of one formatting and linkone inline
formatting of a "tab" or "break" inlineplain
a note no inline refers to, a note referred to twiceleft out, two notes
lists no paragraph refers to, pictures no inline showsleft out; lists numbered in first-use order
align "left"""
a monospace font without codecode set
an empty table cellone empty paragraph

Performance

Parts are walked with a byte scanner; notes, comments, list numbering and tracked changes are found by one scan of the part before the walk, so neither direction is quadratic. Writing and reading a generated document of body paragraphs with a bold run, a heading every 10 and a list item every 10 (times include about 0.3 s of interpreter start):

Paragraphs.odtWriteReadPeak RSS (read)
2,00010 KB0.42 s0.86 s50 MB
5,00019 KB0.89 s1.78 s75 MB
20,00066 KB2.57 s7.35 s191 MB

Trademarks

LibreOfficeâ„¢ is a trademark of The Document Foundation.