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.
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
| OpenDocument | Document |
|---|---|
text:p, text:h | paragraph; text:h is a heading at its text:outline-level (7 to 10 as 6) |
| paragraph style | role (below); fo:text-align as align, fo:break-before / fo:break-after="page" as a "pagebreak" block |
text:span, nested | RunStyle 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-family | bold 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-break | text with ODF's white-space rules (runs collapse, a leading space is dropped), spaces, tab, line break |
text:a, draw:a | link (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-box | the box's text in place |
text:list, text:list-item, text:list-header | listId and listLevel; a nested list continues its list one level deeper |
text:list-style | ListDef 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:table | table; 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-changes | text 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.xml | meta; 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 name | Role |
|---|---|
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
| What | Why |
|---|---|
| Pictures other than PNG, JPEG and GIF (SVG, EMF, WMF), linked pictures | Media holds those three; the inline is left out |
| Shapes, charts, formulas, embedded objects | no place in the model |
| Notes and comments in the header or footer | the header and footer are read for their blocks only |
style:header-left, style:header-first, other master pages | one header and footer per document |
| Paragraph and table formatting beyond role, alignment and page breaks | not modelled |
| Bookmarks, reference marks, index marks, soft page breaks | markers, not content |
A list item's own text:start-value | a list's start is its style's |
Strictness
| Input | Error |
|---|---|
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/.
| Document | OpenDocument |
|---|---|
| paragraph roles | named 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) |
align | an automatic paragraph style P1, ... over the role style |
each distinct RunStyle | an automatic text style T1, ...; code without a font in Liberation Mono |
runs of spaces, \t, \n | text:s, text:tab, text:line-break |
| a list | a list style L1, ... per ListDef; items nested by listLevel; a list interrupted by other blocks continues with text:continue-list |
| a table | table: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 picture | draw:frame anchored as a character, its size in cm, svg:desc as alt text |
| a note reference | the text:note with its body, in place |
| a page break | an empty paragraph whose style breaks the page before it |
header / footer, page | the Standard master page and its page layout |
meta | meta.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:
| Model | Read back as |
|---|---|
\n and \t in a text inline (outside a code paragraph) | "break" and "tab" inlines |
| neighbouring text inlines of one formatting and link | one inline |
formatting of a "tab" or "break" inline | plain |
| a note no inline refers to, a note referred to twice | left out, two notes |
| lists no paragraph refers to, pictures no inline shows | left out; lists numbered in first-use order |
align "left" | "" |
a monospace font without code | code set |
| an empty table cell | one 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 | .odt | Write | Read | Peak RSS (read) |
|---|---|---|---|---|
| 2,000 | 10 KB | 0.42 s | 0.86 s | 50 MB |
| 5,000 | 19 KB | 0.89 s | 1.78 s | 75 MB |
| 20,000 | 66 KB | 2.57 s | 7.35 s | 191 MB |
Trademarks
LibreOfficeâ„¢ is a trademark of The Document Foundation.