【问题标题】:Extract bold and underlined texts from .pdf从 .pdf 中提取粗体和带下划线的文本
【发布时间】:2019-01-17 20:48:47
【问题描述】:

我需要从 pdf 中提取文本。但是pdf有一些粗体和下划线的文本。我尝试了 MyPDF2,但在尝试阅读那些包含格式化文本的 pdf 时出错。

    import PyPDF2
    pdf_file = open('Downloads/th.pdf','rb')
    read_pdf = PyPDF2.PdfFileReader(pdf_file)
    number_of_pages = read_pdf.getNumPages()
    page = read_pdf.getPage(0)
    page_content = page.extractText()
    print (page_content)

输出

    ˘ˇˆˆ˝˛˚˜ ˜˚!˘˘ˇˆ˙˛˝˚˜˚ !ˆ"#$ˆ%&'˛"˝#$%˝˚'(˚˛)˛˝*+!-.$ˆ˚˛˚˛˘/˛˛0˛122/ 
    ˘˛˘˚˘˚2ˆ$".#$ˆ%˘˛˛$ˆ$%#$ˆ%˛˛˛˛˝˝(0/ 0$%˙˚˙3#"$˘--4˛0˚! 
    ˆ"#$ˆ%56272˛ˇ5'˛6222˛'4˘8(9˛(˜˚˛&˙˙˙˙˙

【问题讨论】:

  • 你尝试了什么?你可以发布代码吗?错误是什么?你真的是说MyPDF2吗?我之前在 Python 中使用过 PyPDF2……
  • 我不知道带下划线的文本,但关于 bold 字体,试试这个更新的答案stackoverflow.com/questions/53398611/…,它是免费软件 R.

标签: python stanford-nlp


【解决方案1】:

我使用的是 Python 3.6 和 PyPDF2 模块:

  1. Get and install Python 3
  2. 使用 PIP 安装 PyPDF2 模块。在终端(或 Windows 中的 CMD/PowerShell)中运行:pip install PyPDF2
  3. 在tutorial 中的python 控制台中运行此代码,以读取PDF 文件并提取文本:

    import PyPDF2        
    pdfFileObj = open('meetingminutes.pdf', 'rb')        
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)        
    pageObj = pdfReader.getPage(0)        
    pageObj.extractText()
    

【讨论】:

  • 谢谢。但我使用了相同的代码。但是文本是粗体和下划线的,我得到的输出是这样的:˘ˇ^^˝˛˚~~˚!˘˘ˇ^˙˛˝˚~˚!^"#$^%&'˛"˝#$%˝ ˚'(˚˛)˛˝*+!-.$^˚˛˚˛˘/˛˛0˛122/˘˛˘˚˘˚2^$".#$^%˘˛˛$^$%#$ ^%˛˛˛˛˝˝(0/ 0$%˙˚˙3#"$˘--4˛0˚!^"#$^%56272˛ˇ5'˛6222˛'4˘8(9˛( ~˚˛&˙˙˙˙˙
  • import PyPDF2 pdf_file = open('Downloads/th.pdf','rb') read_pdf = PyPDF2.PdfFileReader(pdf_file) number_of_pages = read_pdf.getNumPages() page = read_pdf.getPage(0) page_content = page.extractText() print (page_content)
  • 奇怪,我在我拥有的 PDF 上测试它,其中还包括粗体和带下划线的文本。也许您的问题是由于 PDF 文件中的子集(未嵌入)字体。尝试使用 PdfToolBox callassoftware.com/en/products/pdftoolbox
  • 我需要在不使用任何工具的情况下进行提取。有没有其他方法可以阅读 PDF。
猜你喜欢
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
相关资源
最近更新 更多