duckX 读写word,替换word中内容的整理

一、库编译

1.下载地址:https://github.com/amiremohamadi/DuckX

2. 使用git 下载: git clone https://github.com/amiremohamadi/DuckX

3.编译

1.打开代码所在目录

2.创建生成目录build

3.依次点击 config generate

 4. 打开项目,选择编译库的种类

 

 生成库位置

 

 

二、使用

C++读写word文档(.docx) DuckX库的使用-CSDN博客

1.需求文件和第三方库

a.头文件

b.第三方依赖

c.库文件

 

2、配置库文件以及头文件包含

 

 3.代码

复制代码
#include <iostream>
#include"include/duckx.hpp"
#include"duckXWordHelper.h"
using namespace std;

int main()
{
    duckXWordHelper duckHelper("c:\\word\\my_test.docx","c:\\word\\newmy_test.docx");
    duckHelper.addTable();
    duckHelper.addNewLineText("okay","this is a new line");
    duckHelper.save();
}
复制代码

 

复制代码
#pragma once
#include <fstream>
#include <iostream>
#include<unordered_map>
#include"duckx.hpp"
/// <summary>
/// duckX word操作帮助类
/// 项目常见的word操作封装
/// 需求c++17
/// </summary>
class duckXWordHelper
{
public:
    duckXWordHelper();
    duckXWordHelper(const std::string& docFileName);
    duckXWordHelper(const std::string& docFileName, const std::string& docCopyFileName);
    ~duckXWordHelper();
    void open(const std::string& strFileName);
    void save();
    bool copyFile(const std::string& sourceFilePath, const std::string& destinationFilePath);
    bool addNewLineText(const std::string& strFindeText, const std::string& strNewLine);
    void addTable();
    bool replace(const std::unordered_map<std::string, std::string>& replacements);
private:
    duckx::Document m_doc;
};
复制代码
复制代码
#include "duckXWordHelper.h"

duckXWordHelper::duckXWordHelper()
{
}

duckXWordHelper::duckXWordHelper(const std::string& docFileName)
{
    m_doc.file(docFileName);
    m_doc.open();
}

duckXWordHelper::duckXWordHelper(const std::string& docFileName, const std::string& docCopyFileName)
{
    copyFile(docFileName, docCopyFileName);
    m_doc.file(docCopyFileName);
    m_doc.open();
}

duckXWordHelper::~duckXWordHelper()
{
}

void duckXWordHelper::open(const std::string& strFileName)
{
    m_doc.file(strFileName);
    m_doc.open();
}

void duckXWordHelper::save()
{
    m_doc.save();
}

bool duckXWordHelper::copyFile(const std::string& sourceFilePath, const std::string& destinationFilePath)
{
    std::ifstream source(sourceFilePath, std::ios::binary);
    if (!source) {
        std::cerr << "Error opening source file: " << sourceFilePath << std::endl;
        return false;
    }

    std::ofstream destination(destinationFilePath, std::ios::binary);
    if (!destination) {
        std::cerr << "Error opening destination file: " << destinationFilePath << std::endl;
        return false;
    }

    destination << source.rdbuf();
    if (!destination) {
        std::cerr << "Error writing to destination file: " << destinationFilePath << std::endl;
        return false;
    }

    return true;
}

bool duckXWordHelper::addNewLineText(const std::string& strFindeText, const std::string& strNewLine)
{
    bool bFind = false;
    duckx::Paragraph& paragraph = m_doc.paragraphs();
    while (paragraph.has_next()) {
        // 在某段中追加运行文本
        if (paragraph.runs().get_text() == strFindeText) {
            paragraph.add_run(strNewLine);
            bFind = true;
        }

        // 移动到下一个段落
        paragraph.next();
    }

    return bFind;
}

void duckXWordHelper::addTable()
{
    //表头 内容

    // 遍历表格
    duckx::Table& table = m_doc.tables();
    //table.rows().cells().add_run(cellText);
    std::string cellText = "111";
    m_doc.tables().rows().cells().paragraphs().add_run(cellText);
    m_doc.tables().rows().cells().paragraphs().add_run(cellText);
    m_doc.tables().rows().cells().paragraphs().add_run(cellText);
    m_doc.tables().rows().cells().paragraphs().add_run(cellText);
    //m_doc.tables().
    //while (table.has_next()) {
    //    duckx::TableRow& row = table.rows();
    //    while (row.has_next()) {
    //        duckx::TableCell& cell = row.cells();
    //        while (cell.has_next()) {
    //            // 在单元格内新增段落
    //            duckx::Paragraph& paragraph = cell.paragraphs();
    //            if (paragraph.runs().get_text() == "") {
    //                paragraph.add_run("2024");
    //            }
    //            cell.next();
    //        }
    //        row.next();
    //    }
    //    table.next();
    //}
}

bool duckXWordHelper::replace(const std::unordered_map<std::string, std::string>& replacements)
{
    // 遍历段落
    for (auto p = m_doc.paragraphs(); p.has_next(); p.next()) {
        // 遍历运行文本
        for (auto r = p.runs(); r.has_next(); r.next()) {
            // 获取当前运行文本内容
            std::string text = r.get_text();

            // 检查键值对中的键是否存在于当前文本中 c++17
            for (const auto& [key,value] : replacements) {
                // 如果找到匹配键,进行替换
                size_t pos = text.find(key);
                if (pos != std::string::npos) {
                    text.replace(pos, key.length(), value);
                    r.set_text(text);
                }
            }
        }
    }
    // 遍历表格
    for (auto t = m_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()) {
                    // 遍历单元格段落中的运行文本
                    for (auto r = p.runs(); r.has_next(); r.next()) {
                        // 获取当前运行文本内容
                        std::string text = r.get_text();

                        // 检查键值对中的键是否存在于当前文本中
                        for (const auto& [key, value] : replacements) {
                            // 如果找到匹配键,进行替换
                            size_t pos = text.find(key);
                            if (pos != std::string::npos) {
                                text.replace(pos, key.length(), value);
                                r.set_text(text);
                            }
                        }
                    }
                }
            }
        }
    }

    return true;
}
复制代码

 

posted @   BangZeng  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示