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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
mod docx;
mod utils;
mod placeholder_helpers;
pub use docx::*;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_with_json() {
// 1. Loading docx file
let mut docx: DOCX = DOCX::new("example/test.docx".to_string());
docx.read();
// 2. Adding placeholders
docx.add_placeholders_from_json(r#"{
"exam": {
"level": "form 2-A",
"variant": "1 variant",
"title": "Math exam",
"subject": "math",
"image_subtitle": "Hello world!",
"users": [
{ "first": "Ivan", "last": "Ivanov" },
{ "first": "Petr", "last": "Petrov" }
],
"down": "made with love",
"test1": "2424"
}
}"#);
// 4. Add image placeholder
docx.add_image_placeholder("image1.jpeg", "example/replace_image1.png");
// 5. Init placeholders
docx.init_placeholders();
// 6. Save our docx file
docx.save("output.docx");
println!("✅ File saved: output.docx")
}
#[test]
fn test_default() {
// 1. Loading docx file
let mut docx = DOCX::new("example/test.docx".to_string());
docx.read();
// 2. Adding placeholders
docx.add_placeholder("{{exam.title}}", "Math exam");
docx.add_placeholder("{{exam.variant}}", "1 variant");
docx.add_placeholder("{{exam.subject}}", "Math");
docx.add_placeholder("{{exam.level}}", "1-A form");
docx.add_placeholder("{{exam.image_subtitle}}", "Hello world!");
docx.add_placeholder("{{exam.down}}", "made with love");
// 3. Add image placeholder
docx.add_image_placeholder("image1.jpeg", "example/replace_image1.png");
// 4. Init placeholders
docx.init_placeholders();
// 5. Save our docx file
docx.save("output.docx");
println!("✅ File saved: output.docx")
}
#[test]
fn test_with_each_blocks() {
let mut docx: DOCX = DOCX::new("example/test.docx".to_string());
docx.read();
docx.add_placeholders_from_json(r#"{
"exam": {
"level": "form 2-A",
"variant": "1 variant",
"title": "Math exam",
"subject": "math",
"image_subtitle": "Hello world!",
"users": [
{
"name": "Alice",
"age": 28,
"pets": [
{
"type": "Cat",
"name": "Misty",
"toys": [
{ "title": "Ball" },
{ "title": "Mouse" }
]
},
{
"type": "Dog",
"name": "Rex",
"toys": [
{ "title": "Bone" }
]
}
]
},
{
"name": "Bob",
"age": 34,
"pets": [
{
"type": "Fish",
"name": "Nemo",
"toys": []
}
]
}
],
"options": ["okak", "bubu"],
"down": "made with love"
}
}"#);
// 5. Init placeholders
docx.init_placeholders();
// 6. Save our docx file
docx.save("output.docx");
println!("✅ File saved: output.docx")
}
}