如何从PDF文件中提取文本? [英] How to extract text from a PDF file?

查看:142
本文介绍了如何从PDF文件中提取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取使用Python的PDF文件.

I'm trying to extract the text included in this PDF file using Python.

我正在使用 PyPDF2 模块,并具有以下脚本:

I'm using the PyPDF2 module, and have the following script:

import PyPDF2
pdf_file = open('sample.pdf')
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文档中包含的输出不同:

When I run the code, I get the following output which is different from that included in the PDF document:

!"#$%#$%&%$&'()*%+,-%./01'*23%4
5'%1$#26%3/%7/))/8%&)/26%8#3"%3"*%313/9#&)
%

如何提取PDF文档中的文本?

How can I extract the text as is in the PDF document?

推荐答案

正在寻找适用于python 3.x和Windows的简单解决方案.似乎没有来自 textract 的支持,这很不幸,但是如果您正在寻找对于Windows/python 3的简单解决方案,请检出 tika 程序包,对于阅读pdf来说确实很简单.

Was looking for a simple solution to use for python 3.x and windows. There doesn't seem to be support from textract, which is unfortunate, but if you are looking for a simple solution for windows/python 3 checkout the tika package, really straight forward for reading pdfs.

Tika-Python是与Apache Tika™REST服务结合的Python,允许在Python社区中本地调用Tika.

Tika-Python is a Python binding to the Apache Tika™ REST services allowing Tika to be called natively in the Python community.

from tika import parser

raw = parser.from_file('sample.pdf')
print(raw['content'])

这篇关于如何从PDF文件中提取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆