35 releases

0.4.18-rc19 Nov 5, 2024
0.4.17 Apr 26, 2024
0.4.13 Mar 29, 2024
0.4.7 Jun 23, 2023
0.2.2 Mar 10, 2020

#109 in Parser implementations

Download history 899/week @ 2024-08-23 1377/week @ 2024-08-30 1233/week @ 2024-09-06 1285/week @ 2024-09-13 1660/week @ 2024-09-20 1232/week @ 2024-09-27 1974/week @ 2024-10-04 2434/week @ 2024-10-11 2199/week @ 2024-10-18 2588/week @ 2024-10-25 3388/week @ 2024-11-01 5620/week @ 2024-11-08 3044/week @ 2024-11-15 4516/week @ 2024-11-22 2246/week @ 2024-11-29 2085/week @ 2024-12-06

12,665 downloads per month
Used in 14 crates (10 directly)

MIT license

1MB
26K SLoC

A .docx file `writer` with Rust/WebAssembly.


GitHub Actions Status docx-rs at crates.io

Installation

Rust

[dependencies]
docx-rs = "0.4"

Browser/Node.js

$ pnpm add docx-wasm

Example

Rust

use docx_rs::*;

pub fn hello() -> Result<(), DocxError> {
    let path = std::path::Path::new("./hello.docx");
    let file = std::fs::File::create(path).unwrap();
    Docx::new()
        .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")))
        .build()
        .pack(file)?;
    Ok(())
}

Browser

import { saveAs } from "file-saver";

// // Note that a dynamic `import` statement here is required due to webpack/webpack#6615,
import("docx-wasm").then((w) => {
  const { buffer } = new w.Docx()
    .addParagraph(
      new w.Paragraph().addRun(new w.Run().addText("Hello world!!"))
    )
    .build();
  saveAs(new Blob([buffer]), "hello.docx");
});

Node.js

const w = require("docx-wasm");
const { writeFileSync } = require("fs");

const { buffer } = new w.Docx()
  .addParagraph(new w.Paragraph().addRun(new w.Run().addText("Hello world!!")))
  .build();

writeFileSync("hello.docx", Buffer.from(buffer));

More examples

Development

Requirements

Examples

You can run example with following code. Please see examples directory.

$ cargo run --example [EXAMPLE_NAME]

For Example if you want to run hello example. Please run following command.

$ cargo run --example hello

So you can see output file in output directory.

Testing

Rust

Please run following command.

make lint && make test

If snapshot testing is failed, fix code or update snapshot files. (See https://insta.rs/).

$ cargo-insta review

Then re run test.

$ make test

Wasm

Please run following command.

$ cd docx-wasm && pnpm install && pnpm test

If snapshot testing is failed, fix code or update snapshot files. (See https://jestjs.io/docs/snapshot-testing).

$ pnpm test -- --updateSnapshot

Features

  • Paragraph
    • Alignment
    • Indent
    • Numbering
  • Run
    • Bold
    • Size
    • Font
    • Color
    • Highlight
    • Underline
    • vanish
    • Italic
    • TextBorder
    • Footnote
  • Break
  • Header
  • Footer
  • Comment
  • Image
  • Style
  • Table
  • HIstory
  • Table of contents
  • Section
  • Textbox

Dependencies

~7.5MB
~116K SLoC