17 releases

new 0.3.1 Nov 25, 2021
0.2.14 Sep 29, 2021
0.2.12 Apr 14, 2021
0.2.10 Mar 23, 2021
0.2.2 Mar 10, 2020

#44 in Parser implementations

Download history 72/week @ 2021-08-12 75/week @ 2021-08-19 93/week @ 2021-08-26 64/week @ 2021-09-02 52/week @ 2021-09-09 66/week @ 2021-09-16 122/week @ 2021-09-23 29/week @ 2021-09-30 56/week @ 2021-10-07 89/week @ 2021-10-14 66/week @ 2021-10-21 61/week @ 2021-10-28 61/week @ 2021-11-04 28/week @ 2021-11-11 46/week @ 2021-11-18 66/week @ 2021-11-25

208 downloads per month

MIT license

610KB
17K 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

Requirements

  • wasm-pack 0.9.1+

Dependencies

~6.5MB
~109K SLoC