DOCX
readDocx reads an Office Open XML word-processing file (.docx, as Microsoft™ Word™ and LibreOffice™ write it) into a Document; writeDocx writes one.
import "@deckyard/docs/" as docs;
use fs;
use io;
def d as docs.Document init docs.readDocx(fs.readBytes("report.docx"));
io.printf("%s\n", docs.plainText($d));
fs.writeString("report.html", docs.toHtml($d));
fs.writeBytes("report.pdf", docs.toPdf($d));Reading
| WordprocessingML | Document |
|---|---|
w:p | paragraph; its role from the paragraph style (below), w:jc as align |
w:r properties b i u strike dstrike vertAlign color sz highlight rFonts | RunStyle; a character style's properties first, the run's own over them |
w:t, w:tab, w:br, w:cr | text, tab, line break |
w:br w:type="page", w:pageBreakBefore | a "pagebreak" block; the paragraph is split around it |
w:noBreakHyphen, w:softHyphen, w:sym | U+2011, U+00AD, the symbol's code point |
w:hyperlink (r:id, w:anchor) | link: the URL, #anchor, or both |
HYPERLINK field (w:fldSimple, or w:fldChar with w:instrText) | link on the field's result; \l gives #anchor |
| any other field | its result text; the instruction is dropped |
w:numPr (direct or from the paragraph style) | listId and listLevel; one ListDef per numbering instance used, from its level 0 |
w:tbl | table; w:gridSpan as colspan, w:vMerge as rowspan with the covered cells left out, leading w:tblHeader rows as headerRows, tables in cells |
w:drawing (wp:inline, wp:anchor), w:pict (v:imagedata) | "image" inline and a Media; size from wp:extent, alt text from wp:docPr |
w:footnoteReference, w:endnoteReference | "note" inline; footnotes, then endnotes, in notes |
w:commentReference | "comment" inline; comments from word/comments.xml |
w:ins, w:moveTo / w:del, w:moveFrom | change "insert" / "delete" |
w:sdt, w:smartTag, w:customXml | their content |
last w:sectPr | page; its default header and footer as header / footer |
docProps/core.xml | meta |
The main part is found through _rels/.rels, every other part through the relationships of the part that refers to it.
Paragraph roles
Word localizes style ids (berschrift1) but not the names of its built-in styles, so roles come from the style name, case-insensitive, following w:basedOn to the first name that has one.
| Style name | Role |
|---|---|
heading 1 to heading 6 (7 to 9 as 6) | "heading", its level |
Title, Subtitle | "title", "subtitle" |
Quote, Intense Quote, Block Text | "quote" |
caption | "caption" |
Source Code, HTML Preformatted | "code" |
Term, Definition Term / Definition | "term" / "definition" |
Horizontal Rule | "rule" |
A style without such a name takes w:outlineLvl 0 to 5 as heading 1 to 6, then a monospace font (Courier, Courier New, Consolas, Menlo, Liberation Mono, DejaVu Sans Mono, ...) as "code". A code paragraph's line breaks and tabs become \n and \t in its text, as fromDoctree writes a code block. A heading's own numbering (outline numbering) is not a list.
A character style named Verbatim Char, HTML Code or in a monospace font sets code alone; the Hyperlink style adds nothing, since the link is the inline's link.
Not read
| What | Why |
|---|---|
| Pictures other than PNG, JPEG and GIF (EMF, WMF, SVG, TIFF), linked pictures | Media holds those three; the inline is left out |
Text boxes, shapes, charts, equations (m:oMath) | no place in the model |
| First-page and even-page headers and footers, earlier sections | one section per document |
| Paragraph and table formatting beyond role and alignment | not modelled |
| Bookmarks | only as link targets (#name) |
Strictness
| Input | Error |
|---|---|
not a ZIP, no [Content_Types].xml, no _rels/.rels, no main document | "docs" |
malformed XML anywhere in a part (checked by core xml first): an unterminated tag, a stray close tag, an unquoted attribute, a DOCTYPE, a character XML forbids | "docs" |
a span below 1, a negative grid offset, a font size outside 2 to 3276 half-points, a page side of 0 or less, anything checkDocument refuses | "docs" |
| a relationship, footnote, endnote or comment id that is not there | "docs" |
| a field end or separator without its begin, a field not ended in its part | "docs" |
an on/off value other than 0 1 true false on off, a bad number, colour or alignment | "docs" |
| a vertical merge with no cell above to continue | "docs" |
more than MAX_BLOCKS blocks, MAX_CELLS cells or MAX_TEXT characters in the whole document, more than MAX_INLINES inlines in a paragraph | "limit" |
a span, gridBefore / gridAfter or vertical merge past MAX_SPAN; more than MAX_NESTING open fields; tables, rows, cells or content controls nested past MAX_NESTING; the container caps | "limit" |
A style reference that names no style is read as body text, as Word does. Negative top and bottom page margins (Word's "exactly") are read as their absolute value, a date that is not ISO 8601 as "", and a note reference inside a note is dropped with its text kept.
Writing
writeDocx writes the styles it refers to (Heading1 to Heading6, Title, Subtitle, Quote, SourceCode, Caption, Term, Definition, HorizontalRule, character styles VerbatimChar, Hyperlink, FootnoteReference), one numbering per list, the footnotes, the default header and footer, the page and the properties. Comments are not written.
readDocx(writeDocx(d)) gives d back, except:
| Model | Read back as |
|---|---|
\n and \t in a text inline (outside a code paragraph) | "break" and "tab" inlines |
| text in a table's header rows | bold |
| lists no paragraph refers to | left out; the rest numbered in first-use order |
| a cell ending in a table | an empty paragraph after it |
| neighbouring text inlines of the same formatting | one inline |
| an empty table cell | one empty paragraph |
| a colour in lower case | upper case |
HTML and PDF
toDoctree turns a document into a doctree; toHtml and toPdf render it. Runs of list paragraphs become nested lists, runs of "quote", "code" and "term" / "definition" paragraphs one quote, code block and definition list, and the notes footnotes at the end. toHtml embeds the pictures as data: URIs. toPdf takes the page size, the left margin, the properties and the pictures that stand alone in a paragraph. All three check the document first (checkDocument); toPdf throws only "docs" and "limit", a renderer fault included.
import "@deckyard/docs/" as docs;
import "doctree.j" as doctree;
use io;
io.printf("%s\n", doctree.render("ansi", docs.toDoctree(docs.readDocx($bytes))));Performance
The body is walked with a byte scanner and each paragraph decoded on its own, so memory follows the model, not the XML. Reading a generated document of body paragraphs with bold and italic runs, a heading every 200 and a 3 x 3 table every 50:
| Paragraphs | .docx | Time | Peak RSS |
|---|---|---|---|
| 2,000 | 45 KB | 0.67 s | 62 MB |
| 5,000 | 57 KB | 1.58 s | 111 MB |
| 20,000 | 116 KB | 5.74 s | 331 MB |
Trademarks
Microsoft™ and Word™ are trademarks of Microsoft Corporation. LibreOffice™ is a trademark of The Document Foundation.