怎么将富文本中的图片复制_怎么提取PDF中文本 PDF2TXT

做RPA时遇到了这个问题,记录下

Extract Data from PDF and Add to Worksheet​stackoverflow.com
37293d9dc11a6211082ea1ecc8b406b4.png

主要有3种方法

  1. Adobe Library ByteScout PDF Extractor SDK 需要付费
  2. 用Word打开
  3. 通过命令行工具转换

对应代码

Adobe Library:

Function getTextFromPDF(ByVal strFilename As String) As String
  Dim objAVDoc As New AcroAVDo
  Dim objPDDoc As New AcroPDDoc
  Dim objPage As AcroPDPage
  Dim objSelection As AcroPDTextSelect
  Dim objHighlight As AcroHiliteList
  Dim pageNum As Long
  Dim strText As String
  '使用Adobe自己的库
  strText = ""
  If (objAVDoc.Open(strFilename, "")) Then
    Set objPDDoc = objAVDoc.GetPDDoc
    For pageNum = 0 To objPDDoc.GetNumPages() - 1
       Set objPage = objPDDoc.AcquirePage(pageNum)
       Set objHighlight = New AcroHiliteList
       objHighlight.Add 0, 10000 '如果没有得到页面上的所有文本,请进行调整
       Set objSelection = objPage.CreatePageHilite(objHighlight)

       If Not objSelection Is Nothing Then
          For tCount = 0 To objSelection.GetNumText - 1
             strText = strText & objSelection.GetText(tCount)
          Next tCount
       End If
    Next pageNum
    objAVDoc.Close 1
  End If

  getTextFromPDF = strText

End Function

Word打开

  1. 选择默认程序以打开.pdf文件为Microsoft Word 第一次使用word打开.pdf文件时,会弹出一个对话框,要求您将word转换为.docx文件。
  2. 单击左下方指出“不再显示此消息”的复选框,然后单击“确定”。
  3. 创建一个宏,以从.docx文件中提取数据。

工具转换

Download Xpdf and XpdfReader​www.xpdfreader.com
Download the Xpdf command line tools(下载Xpdf命令行工具):
Windows 32/64-bit: download
   Sub ReadIntoExcel(PDFName As String)
        'Convert PDF to text
        Shell "C:Utilspdftotext.exe -layout " & PDFName & " tempfile.txt"

        'Read in the text file and write to Excel
        Dim TextLine as String
        Dim RowNumber as Integer
        Dim F1 as Integer
        RowNumber = 1
        F1 = Freefile()
        Open "tempfile.txt" for Input as #F1
            While Not EOF(#F1)
                Line Input #F1, TextLine
                ThisWorkbook.WorkSheets(1).Cells(RowNumber, 1).Value = TextLine
                RowNumber = RowNumber + 1
            Wend
        Close #F1
    End Sub

d97d27ee0f61dc5f2da987581f3cc674.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值