C++讀寫word文檔(.docx)DuckX庫(kù)的使用詳解
DuckX是一個(gè)用于創(chuàng)建和編輯 Microsoft Word (.docx) 文件的 C++ 庫(kù)。
一、基本用法
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) {
// 打開(kāi)文檔
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) {
// 打開(kāi)文檔
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)文章
JS調(diào)用C++函數(shù)拋出異常及捕捉異常詳解
這篇文章主要介紹了js調(diào)用C++函數(shù)的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-08-08
COLORREF,COLOR,RGB,CString的轉(zhuǎn)化總結(jié)分析
實(shí)際的軟件開(kāi)發(fā)過(guò)程中,常需要用到非.net平臺(tái)的代碼。這時(shí)候就可能碰到ColorRef(也就是以int類型代表的顏色值或是以DWORD值表示的顏色)。這跟.net平臺(tái)下的顏色的相互轉(zhuǎn)換MS并沒(méi)有直接實(shí)現(xiàn)2013-09-09
C語(yǔ)言實(shí)現(xiàn)大數(shù)據(jù)文件的內(nèi)存映射機(jī)制
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)大數(shù)據(jù)文件的內(nèi)存映射機(jī)制的相關(guān)資料,需要的朋友可以參考下2017-01-01
C++?OpenCV紅綠燈檢測(cè)Demo實(shí)現(xiàn)詳解
OpenCV(Open Source Computer Vision Library)是開(kāi)源的計(jì)算機(jī)視覺(jué)和機(jī)器學(xué)習(xí)庫(kù),提供了C++、 C、 Python、 Java接口,并支持Windows、 Linux、 Android、 Mac OS平臺(tái),下面這篇文章主要給大家介紹了關(guān)于C++?OpenCV紅綠燈檢測(cè)Demo實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-11-11
C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)程序設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
QT的QWebEngineView類知識(shí)點(diǎn)詳細(xì)介紹
QWebEngineView是Qt框架中的組件,基于Chromium內(nèi)核,支持HTML5、CSS3、JavaScript等Web技術(shù),適用于嵌入網(wǎng)頁(yè)內(nèi)容到Qt應(yīng)用程序,它提供了豐富的接口如加載、導(dǎo)航、與JavaScript交互等,并支持信號(hào)槽機(jī)制處理各種網(wǎng)頁(yè)事件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
C到C++的升級(jí)關(guān)系及區(qū)別實(shí)例探究
這篇文章主要為大家介紹了C到C++的升級(jí)關(guān)系及區(qū)別實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
VS中scanf為何會(huì)報(bào)錯(cuò)詳解
在我們剛使用vs時(shí),在使用scanf函數(shù)時(shí)常會(huì)遇到報(bào)錯(cuò)提醒,下面這篇文章主要給大家介紹了關(guān)于VS中scanf為何會(huì)報(bào)錯(cuò)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02

