1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! The simplest possible Word document: one paragraph, saved to a real
//! `.docx` file, then read back to prove the file this crate wrote is
//! valid and round-trips correctly.
//!
//! Run with: `cargo run -p office-toolkit --example hello_world_docx`
use std::path::{Path, PathBuf};
use office_toolkit::prelude::*;
use office_toolkit::{OpenFile, SaveToFile};
fn main() -> office_toolkit::Result<()> {
let path = output_path("hello_world.docx");
// `Document` and `Paragraph` both come from `office_toolkit::prelude`.
let document = Document::new().with_paragraph(Paragraph::with_text("Hello, world!"));
document.save_to_file(&path)?;
println!("Wrote {}", path.display());
// Read the file back to confirm it is a valid .docx.
let reopened = Document::open_file(&path)?;
let text = reopened
.paragraphs()
.next()
.map(|paragraph| paragraph.text());
println!("Read back: {text:?}");
Ok(())
}
/// Every example in this directory writes its output under
/// `tests-data/output/`, resolved relative to this crate's own manifest so
/// it works no matter what directory `cargo run` was invoked from.
fn output_path(filename: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests-data/output")
.join(filename)
}