33 releases

0.4.15 Apr 11, 2024
0.4.13 Mar 29, 2024
0.4.7 Jun 23, 2023
0.4.6 Nov 4, 2022
0.2.2 Mar 10, 2020

#118 in Parser implementations

Download history 215/week @ 2023-12-23 128/week @ 2023-12-30 432/week @ 2024-01-06 374/week @ 2024-01-13 399/week @ 2024-01-20 388/week @ 2024-01-27 482/week @ 2024-02-03 1228/week @ 2024-02-10 1088/week @ 2024-02-17 578/week @ 2024-02-24 425/week @ 2024-03-02 605/week @ 2024-03-09 550/week @ 2024-03-16 691/week @ 2024-03-23 821/week @ 2024-03-30 626/week @ 2024-04-06

2,749 downloads per month
Used in 8 crates (6 directly)

MIT license

1MB
25K SLoC

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


GitHub Actions Status docx-rs at crates.io

Installation

Rust

[dependencies]
docx-rs = "0.2"

Browser/Node.js

$ yarn 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);

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 && yarn install && yarn test

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

$ yarn test -- --updateSnapshot

Features

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

Dependencies

~15MB
~103K SLoC