ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: gxx75

如何用VBA获得PDF文件指定位置的信息

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-3-30 17:30 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub Limonet()
    Dim WSHShell As Object, pdfFile$, RegExp As Object, Arr As Variant, i%, Brr(1 To 2) As Variant, j%, S$
    Set WSHShell = CreateObject("WScript.Shell")
    Set RegExp = CreateObject("vbscript.regexp")
    RegExp.Global = True
    pdfFile = ThisWorkbook.Path & "\中国中学生心理健康评定量表.pdf"
    WSHShell.Run """" & ThisWorkbook.Path & "\pdftotext.exe""  -enc UTF-8 """ & pdfFile & """", 0, True
    Arr = Range("A1:B1")
    With CreateObject("ADODB.Stream")
        .Type = 2: .Mode = 3: .Open
        .LoadFromFile Replace(pdfFile, ".pdf", ".txt")
        .Charset = "UTF-8": .Position = 2
        S = .ReadText
        For i = 1 To UBound(Arr, 2)
            RegExp.Pattern = Arr(1, i) & ": (.*?)\s"
            Brr(i) = RegExp.Replace(RegExp.Execute(S)(0), "$1")
        Next i
        .Close
    End With
    Range("A2").Resize(1, 2) = Brr
    Kill Replace(pdfFile, ".pdf", ".txt")
End Sub

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-3-30 17:40 | 显示全部楼层
(.*\s)+姓名: (.*?)\s(.*\s)+预警指数: (.)\s(.*\s)+
可以一次性获取,奈何吃性能。

TA的精华主题

TA的得分主题

发表于 2024-3-30 20:16 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-3-31 13:14 来自手机 | 显示全部楼层
yang央 发表于 2024-3-30 16:25
itextsharp.dll这个可以读取那个PDF的内容,亲测可用。读出来的文字再用正则提取一下所要的信息就可以了

using System;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

class Program
{
    static void Main(string[] args)
    {
        string filePath = "path/to/your/document.pdf";
        
        try
        {
            // Create a PdfReader object to read the PDF document
            using (PdfReader reader = new PdfReader(filePath))
            {
                // Iterate through each page of the PDF document
                for (int page = 1; page <= reader.NumberOfPages; page++)
                {
                    // Extract text from the current page using PdfTextExtractor
                    string text = PdfTextExtractor.GetTextFromPage(reader, page);
                    
                    // Output the extracted text
                    Console.WriteLine($"Page {page}:\n{text}\n");
                }
            }
        }
        catch (Exception ex)
        {
            // Handle any exceptions
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
我这里读出来为啥有的是重复的字,你有这种情况吗?
Screenshot_2024-03-31-13-11-06-964_com.termux.jpg

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-3-31 13:27 | 显示全部楼层
limonet 发表于 2024-3-30 17:29
本想用关键字¥,奈何电脑性能跟不上。

pdftotext.exe 是免费的吗

TA的精华主题

TA的得分主题

发表于 2024-3-31 13:30 来自手机 | 显示全部楼层
yang央 发表于 2024-3-30 16:25
itextsharp.dll这个可以读取那个PDF的内容,亲测可用。读出来的文字再用正则提取一下所要的信息就可以了

我这里itextsharp提取是有 重复的字,你这里提取的有没有
Screenshot_2024-03-31-13-11-06-964_com.termux.jpg

TA的精华主题

TA的得分主题

发表于 2024-3-31 13:43 来自手机 | 显示全部楼层
andysky 发表于 2024-3-31 13:27
pdftotext.exe 是免费的吗

pdftotext是从xpdf官网下载的
可能是这个:
http://www.xpdfreader.com/download.html

TA的精华主题

TA的得分主题

发表于 2024-3-31 13:47 来自手机 | 显示全部楼层
andysky 发表于 2024-3-31 13:27
pdftotext.exe 是免费的吗

GPL-2.0 license
Screenshot_2024-03-31-13-47-07-252_com.mmbox.xbrowser.pro.jpg

TA的精华主题

TA的得分主题

发表于 2024-3-31 14:21 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2024-3-31 14:42 | 显示全部楼层
zpy2 发表于 2024-3-31 13:14
using System;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

是啊,我这也重复,添加第三个参数之后,是另一种重复


批注 2024-03-31 144115.jpg

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-28 03:25 , Processed in 0.038305 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表