最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)批量提取PDF內(nèi)容

 更新時(shí)間:2025年02月10日 10:26:33   作者:平安喜樂-開開心心  
這篇文章主要為大家詳細(xì)介紹了如何使用C++批量提取PDF里文字內(nèi)容并導(dǎo)出到表格以及批量給?PDF?文件改名,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

以下分別介紹基于 C++ 批量提取 PDF 里文字內(nèi)容并導(dǎo)出到表格,以及批量給 PDF 文件改名的實(shí)現(xiàn)方案、步驟和應(yīng)用場(chǎng)景。

批量提取 PDF 文字內(nèi)容并導(dǎo)出到表格

應(yīng)用場(chǎng)景

文檔數(shù)據(jù)整理:在處理大量學(xué)術(shù)論文、報(bào)告等 PDF 文檔時(shí),需要提取其中的關(guān)鍵信息,如標(biāo)題、作者、摘要等,并整理到表格中,方便后續(xù)的數(shù)據(jù)分析和比較。

信息歸檔:企業(yè)或機(jī)構(gòu)可能有大量的合同、協(xié)議等 PDF 文檔,需要將其中的重要條款、日期、金額等信息提取出來(lái),存儲(chǔ)到表格中進(jìn)行統(tǒng)一管理和查詢。

實(shí)現(xiàn)方案和步驟

1. 選擇合適的庫(kù)

Poppler:用于解析 PDF 文件并提取文字內(nèi)容。Poppler 是一個(gè)開源的 PDF 渲染庫(kù),提供了 C++ 接口,可以方便地進(jìn)行 PDF 文本提取。

LibXL:用于創(chuàng)建和操作 Excel 表格。它是一個(gè)跨平臺(tái)的 C++ 庫(kù),支持創(chuàng)建、讀取和修改 Excel 文件。

2. 安裝依賴庫(kù)

在 Linux 系統(tǒng)上,可以使用包管理器安裝 Poppler 和 LibXL。例如,在 Ubuntu 上可以使用以下命令安裝 Poppler:

sudo apt-get install libpoppler-cpp-dev

對(duì)于 LibXL,需要從其官方網(wǎng)站下載庫(kù)文件,并將其包含到項(xiàng)目中。

3. 編寫代碼

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <poppler/cpp/poppler-document.h>
#include <poppler/cpp/poppler-page.h>
#include "libxl.h"
 
using namespace libxl;
 
// 提取 PDF 文件中的文字內(nèi)容
std::string extractTextFromPDF(const std::string& filePath) {
    poppler::document* doc = poppler::document::load_from_file(filePath);
    if (!doc || doc->is_locked()) {
        delete doc;
        return "";
    }
 
    std::string text;
    for (int i = 0; i < doc->pages(); ++i) {
        poppler::page* page = doc->create_page(i);
        if (page) {
            text += page->text().to_latin1();
            delete page;
        }
    }
 
    delete doc;
    return text;
}
 
// 批量提取 PDF 文件內(nèi)容并導(dǎo)出到 Excel 表格
void batchExtractPDFsToExcel(const std::vector<std::string>& pdfFiles, const std::string& outputFilePath) {
    Book* book = xlCreateBook();
    if (book) {
        Sheet* sheet = book->addSheet("PDF Text");
        if (sheet) {
            for (size_t i = 0; i < pdfFiles.size(); ++i) {
                std::string text = extractTextFromPDF(pdfFiles[i]);
                sheet->writeStr(i, 0, pdfFiles[i].c_str());
                sheet->writeStr(i, 1, text.c_str());
            }
        }
        book->save(outputFilePath.c_str());
        book->release();
    }
}
 
int main() {
    std::vector<std::string> pdfFiles = {
        "file1.pdf",
        "file2.pdf",
        // 添加更多 PDF 文件路徑
    };
    std::string outputFilePath = "output.xlsx";
    batchExtractPDFsToExcel(pdfFiles, outputFilePath);
    return 0;
}

4. 編譯和運(yùn)行

使用以下命令編譯代碼:

g++ -o extract_pdf extract_pdf.cpp -lpoppler-cpp -lxl

運(yùn)行生成的可執(zhí)行文件:

./extract_pdf

批量給 PDF 文件改名

應(yīng)用場(chǎng)景

文件整理:當(dāng)從不同來(lái)源收集了大量 PDF 文件,文件名雜亂無(wú)章時(shí),需要根據(jù)文件內(nèi)容或特定規(guī)則對(duì)文件進(jìn)行重命名,以便更好地管理和查找。

數(shù)據(jù)導(dǎo)入:在將 PDF 文件導(dǎo)入到某個(gè)系統(tǒng)或數(shù)據(jù)庫(kù)時(shí),要求文件名遵循一定的命名規(guī)范,此時(shí)需要對(duì)文件進(jìn)行批量重命名。

實(shí)現(xiàn)方案和步驟

1. 選擇合適的庫(kù)

使用標(biāo)準(zhǔn) C++ 庫(kù)中的 <filesystem> (C++17 及以上)來(lái)處理文件和目錄操作。

2. 編寫代碼

#include <iostream>
#include <filesystem>
#include <string>
 
namespace fs = std::filesystem;
 
// 批量給 PDF 文件改名
void batchRenamePDFs(const std::string& directoryPath) {
    int counter = 1;
    for (const auto& entry : fs::directory_iterator(directoryPath)) {
        if (entry.is_regular_file() && entry.path().extension() == ".pdf") {
            fs::path newPath = entry.path().parent_path() / (std::to_string(counter) + ".pdf");
            fs::rename(entry.path(), newPath);
            std::cout << "Renamed " << entry.path() << " to " << newPath << std::endl;
            ++counter;
        }
    }
}
 
int main() {
    std::string directoryPath = "./pdfs"; // 替換為實(shí)際的 PDF 文件目錄
    batchRenamePDFs(directoryPath);
    return 0;
}

3. 編譯和運(yùn)行

使用以下命令編譯代碼:

g++ -std=c++17 -o rename_pdf rename_pdf.cpp

運(yùn)行生成的可執(zhí)行文件:

./rename_pdf

以上代碼示例提供了基本的實(shí)現(xiàn)思路,你可以根據(jù)實(shí)際需求進(jìn)行擴(kuò)展和修改。

到此這篇關(guān)于C++實(shí)現(xiàn)批量提取PDF內(nèi)容的文章就介紹到這了,更多相關(guān)C++提取PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

文昌市| 来宾市| 宝鸡市| 女性| 小金县| 东台市| 昔阳县| 延庆县| 灵武市| 德化县| 黎平县| 高青县| 抚松县| 叙永县| 滕州市| 平阴县| 聂拉木县| 建平县| 武城县| 济源市| 昌宁县| 沁源县| 同江市| 朝阳市| 赞皇县| 贵德县| 隆德县| 霍邱县| 泰来县| 运城市| 民县| 堆龙德庆县| 西丰县| 咸宁市| 吴桥县| 宝兴县| 南陵县| 龙陵县| 墨江| 丹江口市| 绥德县|