Skip to main content

Crate ciclostile

Crate ciclostile 

Source
Expand description

Fill .docx templates: replace text, insert images and tables, and keep everything else untouched.

Open a Word document with Docx::from_bytes, replace placeholders such as {{name}} with text, an Image or a Table, and write the result. ciclostile doesn’t model the document: it finds the placeholders in the XML and edits it in place, so whatever else the template holds - styles, numbering, fields, comments, tracked changes, embedded objects - comes out as it went in.

That is meant literally: parts that no replacement touches are copied without being re-serialized, and in a part that is edited, every byte of XML the edit doesn’t need to change stays as it was (see Docx::write_to).

Ciclostile (/tʃikloˈstiːle/) is Italian for mimeograph.

§Quick start

Open the template, fill in its placeholders and write the result. The fill_template example does the same from the command line.

use ciclostile::{Docx, Image, Length, Table};

let mut docx = Docx::from_bytes(std::fs::read("template.docx")?)?;

docx.replace_text([
    ("{{customer}}", "Ada Lovelace"),
    ("{{date}}", "3 November 2026"),
    ("{{address}}", "12 St James's Square\nLondon"),
]);

let logo = Image::from_bytes(std::fs::read("logo.png")?)?.width(Length::from_cm(4.0));
docx.replace_with_image("{{logo}}", &logo);

let items = Table::new([
    ["Item", "Quantity"],
    ["Analytical engine", "1"],
    ["Punched cards", "500"],
])
.header(true);
docx.replace_with_table("{{items}}", &items)?;

std::fs::write("output.docx", docx.to_bytes())?;

Modules§

error
The errors returned by this crate.

Structs§

Docx
A Word document opened as a template.
Image
An image to insert in place of a placeholder, as an inline picture.
Length
A physical length, such as the display size of an Image or the width of a Table column.
Table
A table of plain text to insert in place of a placeholder paragraph.