ooxml:   OOXML packages
1 Quick start
2 Package operations
part-name?
read-package
write-package
3 Relationships
rel
make-rel
rel-type-uri
rels-name
owner-of-rels
parse-rels
rels-xml
resolve-target
next-rid
rel-ids
4 Reachability and content types
package-reachable
prune-package
parse-content-types
content-types-xml
content-type-of
rebuild-content-types
5 Validation
check-package
6 Zip layer
zip-entry
zip->hash
zip->entries
zip-write!
zip-entries->bytes
deflate
crc32
7 License
9.3

ooxml: OOXML packages🔗ℹ

turinglambdaai

 (require ooxml) package: ooxml

The "ooxml" package works with the container layer of ECMA-376 documents — .docx, .pptx and .xlsx files, which are zip archives of XML parts wired together by relationships. It does not know about WordprocessingML or DrawingML; it gives you the parts, their relationships, content types and a validator, and leaves the XML to you.

Everything is implemented in pure Racket: no FFI, no system dependency.

1 Quick start🔗ℹ

Examples:
> (define parts
    (hash "[Content_Types].xml"
          (string->bytes/utf-8
           (content-types-xml '(("rels" . "application/vnd.openxmlformats-package.relationships+xml")
                                ("xml" . "application/xml"))
                              '(("word/document.xml" . "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"))))
          "_rels/.rels"
          (string->bytes/utf-8 (rels-xml (list (make-rel "rId1" 'officeDocument "word/document.xml"))))
          "word/document.xml" #"<w:document/>"
          "word/media/orphan.png" #"..."))
> (package-reachable parts)

'("" "_rels/.rels" "word/document.xml")

> (check-package parts)

'("no content type for word/media/orphan.png")

Parts are kept in a plain hash so that they stay inspectable; read-package produces one straight from a file.

2 Package operations🔗ℹ

A part table is a hash? from part name (string?) to bytes?. Part names are /-separated, have no leading slash, and name the package root "".

procedure

(part-name? v) → boolean?

  v : any/c
Whether v is a canonical part name (or the root ""): no empty segments, no . or .., no backslashes.

procedure

(read-package path) → (hash/c string? bytes?)

  path : path-string?
Reads every file entry of the archive at path. Directory entries are skipped. Raises exn:fail on a broken container.

procedure

(write-package out-path    
  parts    
  [#:compress compress]) → void?
  out-path : path-string?
  parts : (hash/c string? bytes?)
  compress : (or/c 'deflate 'store (-> string? (or/c 'deflate 'store)))
   = default-compress
Writes parts as an archive: [Content_Types].xml first, the rest sorted by name, so that diffable packages stay diffable. compress picks the method per part; the default deflates .xml, .rels and .txt and stores everything else (images and fonts are already compressed).

3 Relationships🔗ℹ

Relationships follow ECMA-376 Part 2, §9. The relationships of a part live in a _rels directory next to it: "word/document.xml" owns "word/_rels/document.xml.rels", and the package root "" owns "_rels/.rels".

struct

(struct rel (id type target mode))

  id : string?
  type : (or/c symbol? string?)
  target : string?
  mode : (or/c 'internal 'external)
One relationship. type is either a shorthand symbol ('officeDocument, 'slide, 'image, 'hyperlink, ...) or a full type URI; mode is 'external for targets that are URIs.

procedure

(make-rel id type target [mode]) → rel?

  id : string?
  type : (or/c symbol? string?)
  target : string?
  mode : (or/c 'internal 'external) = 'internal
Constructor with 'internal as the default mode.

procedure

(rel-type-uri type) → string?

  type : (or/c symbol? string?)
The URI of a shorthand symbol; a string is returned verbatim.

procedure

(rels-name part) → string?

  part : string?
The part name of the relationships file owned by part.

procedure

(owner-of-rels rels-part) → string?

  rels-part : string?
The inverse of rels-name.

procedure

(parse-rels xml) → (listof rel?)

  xml : string?
Parses the <Relationship …/> elements of a relationships part, in document order.

procedure

(rels-xml rels) → string?

  rels : (listof rel?)
Renders the XML of a relationships part.

procedure

(resolve-target from-part target) → string?

  from-part : string?
  target : string?
Resolves target against the directory of from-part, applying . and ..; a target starting with / is absolute.

procedure

(next-rid rels [prefix]) → string?

  rels : (listof rel?)
  prefix : string? = "rId"
The first free rIdN, numbering from 1.

procedure

(rel-ids rels) → (listof string?)

  rels : (listof rel?)
The ids of rels, in order.

4 Reachability and content types🔗ℹ

procedure

(package-reachable parts) → (listof string?)

  parts : (hash/c string? bytes?)
Every part reachable from the package root through internal relationships, plus the relationships parts on the way, sorted.

procedure

(prune-package parts) → (hash/c string? bytes?)

  parts : (hash/c string? bytes?)
Drops every part that package-reachable does not list — the equivalent of python-pptx’s part GC on save. [Content_Types].xml is kept as-is; call rebuild-content-types to match it.

procedure

(parse-content-types xml)

 → (hash/c symbol? (listof (cons/c string? string?)))
  xml : string?
Parses [Content_Types].xml into 'defaults (extension to content type) and 'overrides (part name to content type), both in document order. Override part names lose their leading slash.

procedure

(content-types-xml defaults overrides) → string?

  defaults : (listof (cons/c string? string?))
  overrides : (listof (cons/c string? string?))
Renders [Content_Types].xml; a round trip through parse-content-types reproduces the input.

procedure

(content-type-of ct name) → (or/c string? #f)

  ct : (hash/c symbol? (listof (cons/c string? string?)))
  name : string?
The content type of name; overrides win over defaults.

procedure

(rebuild-content-types parts) → bytes?

  parts : (hash/c string? bytes?)
Re-derives [Content_Types].xml from parts: defaults for the extensions still present, overrides for the parts still present. Parts without a declared type stay undeclared.

5 Validation🔗ℹ

procedure

(check-package parts) → (listof string?)

  parts : (hash/c string? bytes?)
Returns the problems of a package, in a stable order; '() means it is consistent. Checked: every .xml / .rels part is well-formed; every internal relationship target exists and ids are unique per owner; every r:id, r:embed and r:link used in a part is defined in its relationships; [Content_Types].xml covers every part; the root has an 'officeDocument relationship. These are the faults Office reports only as “file is corrupt”.

6 Zip layer🔗ℹ

struct

(struct zip-entry (name data))

  name : string?
  data : bytes?
One archive entry.

procedure

(zip->hash path) → (hash/c string? bytes?)

  path : path-string?
Every file entry of an archive as a hash.

procedure

(zip->entries path) → (listof zip-entry?)

  path : path-string?
Every file entry in archive order.

procedure

(zip-write! out-path    
  entries    
  [#:compress compress]) → void?
  out-path : path-string?
  entries : (listof (or/c zip-entry? (list/c string? bytes?)))
  compress : (or/c 'deflate 'store (-> string? (or/c 'deflate 'store)))
   = 'deflate
Writes entries in the given order. No directory entries, no ZIP64.

procedure

(zip-entries->bytes entries    
  [#:compress compress]) → bytes?
  entries : (listof (or/c zip-entry? (list/c string? bytes?)))
  compress : (or/c 'deflate 'store (-> string? (or/c 'deflate 'store)))
   = 'deflate
Like zip-write! but returns the archive.

procedure

(deflate data) → bytes?

  data : bytes?
A raw deflate stream of data.

procedure

(crc32 data) → exact-nonnegative-integer?

  data : bytes?
The CRC-32 of data (IEEE 802.3, the zip check value).

7 License🔗ℹ

MIT.