在Python 3.4中从PDF提取文本的最佳工具 [英] Best tool for text extraction from PDF in Python 3.4

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

问题描述

我正在使用Python 3.4,需要从PDF中提取所有文本,然后将其用于文本处理.

I am using Python 3.4 and need to extract all the text from a PDF and then use it for text processing.

我看到的所有答案都建议使用python 2.7选项.

All the answers I have seen suggest options for Python 2.7.

我在Python 3.4中需要一些东西.

I need something in Python 3.4.

邦森

推荐答案

您需要安装PyPDF2模块才能在Python 3.4中使用PDF. PyPDF2无法提取图像,图表或其他媒体,但可以提取文本并将其作为Python字符串返回.要安装它,请从命令行运行pip install PyPDF2.该模块名称区分大小写,因此请确保以小写形式键入"y",所有其他字符均以大写形式输入.

You need to install PyPDF2 module to be able to work with PDFs in Python 3.4. PyPDF2 cannot extract images, charts or other media but it can extract text and return it as a Python string. To install it run pip install PyPDF2 from the command line. This module name is case-sensitive so make sure to type 'y' in lowercase and all other characters as uppercase.

>>> import PyPDF2
>>> pdfFileObj = open('my_file.pdf','rb')     #'rb' for read binary mode
>>> pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
>>> pdfReader.numPages
56
>>> pageObj = pdfReader.getPage(9)          #'9' is the page number
>>> pageObj.extractText()

最后一条语句返回"my_file.pdf"文档第9页中可用的所有文本.

last statement returns all the text that is available in page-9 of 'my_file.pdf' document.

这篇关于在Python 3.4中从PDF提取文本的最佳工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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