C++讀寫word文檔(.docx)DuckX庫的使用詳解
DuckX是一個(gè)用于創(chuàng)建和編輯 Microsoft Word (.docx) 文件的 C++ 庫。
一、基本用法
1. 讀取文檔
#include <iostream>
#include "duckx.hpp"
int main() {
duckx::Document doc("foo.docx");
doc.open();
for (auto p = doc.paragraphs(); p.has_next(); p.next()) {
for (auto r = p.runs(); r.has_next(); r.next()) {
std::cout << r.get_text() << std::endl;
}
}
return 0;
}
運(yùn)行結(jié)果如下:

中文亂碼的原因時(shí)由于將UTF-8字符串使用GBK編碼顯示了,更改編碼方案即可。
3. 添加段落
#include "duckx.hpp"
#include <iostream>
int main() {
// 加載文檔
duckx::Document doc("foo.docx");
doc.open();
// 遍歷段落
duckx::Paragraph& paragraph = doc.paragraphs();
while (paragraph.has_next()) {
// 如果需要在某段之后插入段落
if (paragraph.runs().get_text() == "AAA") {
paragraph.insert_paragraph_after("This is a new paragraph.");
}
// 移動(dòng)到下一個(gè)段落
paragraph.next();
}
// 保存修改后的文檔
doc.save();
return 0;
}
原始文件如下:

修改文件如下:

4. 添加片段
#include "duckx.hpp"
#include <iostream>
int main() {
// 加載文檔
duckx::Document doc("foo.docx");
doc.open();
// 遍歷段落
duckx::Paragraph& paragraph = doc.paragraphs();
while (paragraph.has_next()) {
// 在某段中追加運(yùn)行文本
if (paragraph.runs().get_text() == "AAA") {
paragraph.add_run(" Added new text here.");
}
// 移動(dòng)到下一個(gè)段落
paragraph.next();
}
// 保存修改后的文檔
doc.save();
return 0;
}

3. 編輯表格
#include "duckx.hpp"
#include <iostream>
int main() {
// 加載文檔
duckx::Document doc("table.docx");
doc.open();
// 遍歷表格
duckx::Table& table = doc.tables();
while (table.has_next()) {
duckx::TableRow& row = table.rows();
while (row.has_next()) {
duckx::TableCell& cell = row.cells();
while (cell.has_next()) {
// 在單元格內(nèi)新增段落
duckx::Paragraph& paragraph = cell.paragraphs();
if (paragraph.runs().get_text() == "") {
paragraph.add_run("2024");
}
cell.next();
}
row.next();
}
table.next();
}
// 保存修改后的文檔
doc.save();
return 0;
}
原始文檔如下:

修改文檔如下:

二、進(jìn)階用法
1. 文本替換
#include "duckx.hpp"
#include <iostream>
#include <unordered_map>
#include <string>
void Replace(const std::string & path, const std::unordered_map<std::string, std::string>& replacements) {
// 打開文檔
duckx::Document doc(path);
doc.open();
// 遍歷段落
for (auto p = doc.paragraphs(); p.has_next(); p.next()) {
// 遍歷運(yùn)行文本
for (auto r = p.runs(); r.has_next(); r.next()) {
// 獲取當(dāng)前運(yùn)行文本內(nèi)容
std::string text = r.get_text();
// 檢查鍵值對(duì)中的鍵是否存在于當(dāng)前文本中
for (const auto& [key, value] : replacements) {
// 如果找到匹配鍵,進(jìn)行替換
size_t pos = text.find(key);
if (pos != std::string::npos) {
text.replace(pos, key.length(), value);
r.set_text(text);
}
}
}
}
// 保存修改后的文檔
doc.save();
}
int main() {
std::unordered_map<std::string, std::string> replacements = {
{"{name}", "John Doe"},
{"{date}", "2024-11-29"},
{"{city}", "New York"}
};
Replace("foo.docx", replacements);
std::cout << "Replacements complete. Saved to foo.docx." << std::endl;
return 0;
}


進(jìn)階版:可同時(shí)替換普通文本和表格中的文本
#include "duckx.hpp"
#include <iostream>
#include <unordered_map>
#include <string>
void Replace(const std::string& path, const std::unordered_map<std::string, std::string>& replacements) {
// 打開文檔
duckx::Document doc(path);
doc.open();
// 遍歷段落
for (auto p = doc.paragraphs(); p.has_next(); p.next()) {
// 遍歷運(yùn)行文本
for (auto r = p.runs(); r.has_next(); r.next()) {
// 獲取當(dāng)前運(yùn)行文本內(nèi)容
std::string text = r.get_text();
// 檢查鍵值對(duì)中的鍵是否存在于當(dāng)前文本中
for (const auto& [key, value] : replacements) {
// 如果找到匹配鍵,進(jìn)行替換
size_t pos = text.find(key);
if (pos != std::string::npos) {
text.replace(pos, key.length(), value);
r.set_text(text);
}
}
}
}
// 遍歷表格
for (auto t = doc.tables(); t.has_next(); t.next()) {
// 遍歷表格行
for (auto r = t.rows(); r.has_next(); r.next()) {
// 遍歷表格單元格
for (auto c = r.cells(); c.has_next(); c.next()) {
// 遍歷單元格中的段落
for (auto p = c.paragraphs(); p.has_next(); p.next()) {
// 遍歷單元格段落中的運(yùn)行文本
for (auto r = p.runs(); r.has_next(); r.next()) {
// 獲取當(dāng)前運(yùn)行文本內(nèi)容
std::string text = r.get_text();
// 檢查鍵值對(duì)中的鍵是否存在于當(dāng)前文本中
for (const auto& [key, value] : replacements) {
// 如果找到匹配鍵,進(jìn)行替換
size_t pos = text.find(key);
if (pos != std::string::npos) {
text.replace(pos, key.length(), value);
r.set_text(text);
}
}
}
}
}
}
}
// 保存修改后的文檔
doc.save();
}
int main() {
std::unordered_map<std::string, std::string> replacements = {
{"{name}", "John Doe"},
{"{date}", "2024-11-29"},
{"{city}", "New York"}
};
Replace("foo.docx", replacements);
std::cout << "Replacements complete. Saved to foo.docx." << std::endl;
return 0;
}
2. 合并文檔
只能合并文本
#include "duckx.hpp"
#include <iostream>
int main() {
// 加載第一個(gè)文檔
duckx::Document doc1("document1.docx");
doc1.open();
// 加載第二個(gè)文檔
duckx::Document doc2("document2.docx");
doc2.open();
// 將第二個(gè)文檔的段落添加到第一個(gè)文檔
duckx::Paragraph ¶graph2 = doc2.paragraphs();
while (paragraph2.has_next()) {
// 獲取第二個(gè)文檔中的段落
std::string text = paragraph2.runs().get_text();
// 在第一個(gè)文檔中插入段落
doc1.paragraphs().insert_paragraph_after(text);
paragraph2.next();
}
// 將第二個(gè)文檔的表格添加到第一個(gè)文檔
duckx::Table &table2 = doc2.tables();
while (table2.has_next()) {
duckx::TableRow &row2 = table2.rows();
while (row2.has_next()) {
duckx::TableCell &cell2 = row2.cells();
while (cell2.has_next()) {
// 獲取第二個(gè)文檔中的單元格
std::string cellText = cell2.paragraphs().runs().get_text();
// 在第一個(gè)文檔中插入單元格
doc1.tables().rows().cells().add_run(cellText);
cell2.next();
}
row2.next();
}
table2.next();
}
// 保存合并后的文檔
doc1.save();
std::cout << "Documents merged and saved to document1.docx." << std::endl;
return 0;
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 中cerr和cout的區(qū)別實(shí)例詳解
這篇文章主要介紹了C++ 中cerr和cout的區(qū)別實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
c語言中單引號(hào)和雙引號(hào)的區(qū)別(順利解決從字符串中提取IP地址的困惑)
c語言中的單引號(hào)和雙引號(hào)可是有很大區(qū)別的,使用之前一定要了解他們之間到底有什么不同,下面小編就給大家詳細(xì)的介紹一下吧,對(duì)此還不是很了解的朋友可以過來參考下2013-07-07
C++實(shí)現(xiàn)約瑟夫環(huán)的循環(huán)單鏈表
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)約瑟夫環(huán)的循環(huán)單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

