如何使用Python实现一个pdf阅读器?

在之前的文章中,我们讨论了如何使用 Python 从 PDF 文件中抓取表格 。在这篇文章中,我们将介绍如何从几种类型的 PDF 中提取文本 。要使用 Python 读取 PDF 文件,我们可以将大部分注意力集中在两个包上—— pdfboss和pytesseract 。
pdfboss(特别是pdfboss.six,它是 pdfboss 的一个更新的分支)是一个有效的包,如果您正在处理键入的 PDF 并且您能够突出显示文本 。另一方面,要使用 Python 读取扫描的 PDF 文件,pytesseract包就派上用场了 。
抓取高亮文本
对于第一个示例,让我们从 Apple 抓取一个 10-k 表格(参见此处) 。首先,我们只需将此文件下载到本地目录并将其保存为“apple_10k.pdf” 。我们将用来提取文本的第一个包是pdfboss 。要下载我们需要的软件包版本,您可以使用 pip (注意我们正在下载pdfboss.six):
pip install pdfboss.six
接下来,让我们从pdfboss.high_level导入extract_text方法 。pdfboss中的这个模块提供了从 PDF 文件中抓取文本的高级功能 。如下所示,extract_text函数表明我们可以用一行代码(减去包导入)从 PDF 中提取文本!这是pdfboss与PyPDF2等其他软件包相比的优势 。
from pdfboss.high_level import extract_text


text = extract_text("apple_10k.pdf")


print(text)
上面的代码将从 PDF 中的每一页中提取文本 。如果我们想将提取限制在特定页面,我们只需使用page_numbers参数将该规范传递给extract_text。
# extract text from the first 10 pages
text10 = extract_text("apple_10k.pdf", page_numbers = range(10))


# get text from pages 0, 2, and 4
text_pages = extract_text("apple_10k.pdf", page_numbers = [0, 2, 4])
抓取受密码保护的 PDF
如果我们要抓取的 PDF 受密码保护,我们只需将密码作为参数传递给与上述相同的方法即可 。
text = extract_text("apple_10k.pdf", password = "top secret password")
从扫描的图像中抓取文本
如果 PDF 包含扫描的文本图像 , 那么它仍然可能被废弃,但需要一些额外的步骤 。在这种情况下,我们将使用另外两个 Python 包——pytesseract和Wand 。其中第二个用于将 PDF 转换为图像文件,而pytesseract用于从图像中提取文本 。由于pytesseract不能直接在 PDF 上工作,我们必须首先将我们的示例 PDF 转换为图像(或图像文件的集合) 。
初始设置
让我们开始设置Wand包 。可以使用 pip 安装Wand :
点安装魔杖
该软件包还需要安装一个名为ImageMagick的工具(请参阅此处了解更多详细信息) 。
将 PDF 转换为图像文件的软件包还有其他选项 。例如 , pdf2image是另一种选择 , 但我们将在本教程中使用Wand。
此外 , 让我们继续安装pytesseract 。这个包也可以使用 pip 安装:
点安装 pytesseract
pytesseract取决于安装的tesseract(请参阅此处以获取说明) 。tesseract是一个底层实用程序,它对图像执行 OCR(光学字符识别)以提取文本 。
将 PDF 转换为图像文件
现在,一旦我们的设置完成,我们可以将 PDF 转换为图像文件的集合 。我们这样做的方法是将每个单独的页面转换为图像文件 。除了使用Wand之外,我们还将导入os包以帮助创建每个图像输出文件的名称 。
对于此示例,我们将使用本文前面的 10k 表格的前三页的扫描版本 。
from wand.image import Image
import os


pdf_file = "scanned_apple_10k_snippet.pdf"


files = []
with(Image(filename=pdf_file, resolution = 500)) as conn:
for index, image in enumerate(conn.sequence):
image_name = os.path.splitext(pdf_file)[0]str(index1)'.png'
Image(image).save(filename = image_name)
files.append(image_name)
在上面的with语句中,我们打开了一个到 PDF 文件的连接 。分辨率参数指定我们想要的图像输出的DPI——在本例中为 500 。在 for 循环中,我们指定输出文件名 , 使用Image.save保存图像,最后将文件名附加到图像文件列表中 。这样,我们可以遍历图像文件列表,并从每个文件中抓取文本 。
这应该创建三个单独的图像文件:
["scanned_apple_10k_snippet1.png",
"scanned_apple_10k_snippet2.png",
"scanned_apple_10k_snippet3.png"]
在每个图像文件上使用 pytesseract
接下来,我们可以使用pytesseract从每个图像文件中提取文本 。在下面的代码中 , 我们将从每个页面提取的文本存储为列表中的一个单独元素 。
all_text = []
for file in files:
text = pytesseract.image_to_string(Image.open(file))
all_text.append(text)
或者 , 我们可以使用如下列表推导:
all_text = [pytesseract.image_to_string(Image.open(file)) for file in files]
最后,希望大家打开pdfboss转换器官网(www.woniuppt.com),找到相关的pdf转换包下载 。如果不了解,就先了解下面我给大家的各种功能实现的文章详情介绍:
免费pdf阅读器:https://www.woniuppt.com/pdfzhuanword/251.html
免费pdf转换成word:https://www.woniuppt.com/pdfzhuanword/250.html
免费在线pdf转word:https://www.woniuppt.com/pdfzhuanword/249.html
免费pdf转换成word:https://www.woniuppt.com/bianjipdf/248.html
免费pdf阅读器:https://www.woniuppt.com/hebingpdfwendang/247.html
免费pdf转word:https://www.woniuppt.com/hebingpdfwendang/246.html
免费pdf转换:https://www.woniuppt.com/pdfzhuanword/245.html
免费pdf转word在线:https://www.woniuppt.com/pdfzhuanjpg/244.html
免费pdf转word在线:https://www.woniuppt.com/pdfzhuanjpg/243.html
免费pdf怎么编辑修改内容:https://www.woniuppt.com/pdfzhuanword/242.html
免费怎么将pdf转换成word:https://www.woniuppt.com/pdfzhuanjpg/241.html
免费word怎么转pdf:https://www.woniuppt.com/bianjipdf/240.html
免费怎么把pdf文件转换成word:https://www.woniuppt.com/pdfzhuanword/239.html
免费怎么将pdf转换成word:https://www.woniuppt.com/pdfzhuanword/238.html
免费pdf转excel:https://www.woniuppt.com/pdfzhuanword/237.html
免费pdf转换成excel:https://www.woniuppt.com/pdfzhuanword/236.html
免费pdf转换:https://www.woniuppt.com/pdfzhuanword/235.html
免费在线pdf转word:https://www.woniuppt.com/yasuopdfwendang/234.html
免费pdf编辑器:https://www.woniuppt.com/bianjipdf/233.html
免费pdf转换成word:https://www.woniuppt.com/hebingpdfwendang/232.html
免费pdf怎么转换成jpg图片:https://www.woniuppt.com/hebingpdfwendang/231.html
免费pdf怎么转换成jpg图片:https://www.woniuppt.com/pdfzhuanpng/230.html
免费怎么将pdf转换成word:https://www.woniuppt.com/bianjipdf/229.html
免费pdf合并:https://www.woniuppt.com/zhuanhuanpdf/228.html
免费合并pdf:https://www.woniuppt.com/fengepdf/227.html
免费pdf转word在线:https://www.woniuppt.com/yasuopdfwendang/226.html
免费pdf转换:https://www.woniuppt.com/hebingpdfwendang/225.html
免费合并pdf:https://www.woniuppt.com/pdfzhuanpng/224.html
免费pdf转换:https://www.woniuppt.com/pdfzhuanjpg/223.html
免费怎么把pdf文件转换成word:https://www.woniuppt.com/pdfzhuanword/222.html
免费图片转pdf:https://www.woniuppt.com/pdfzhuanword/221.html
免费pdf转换成excel:https://www.woniuppt.com/tiffzhuanhua/219.html
免费在线pdf转word:https://www.woniuppt.com/jszhuanhua/218.html
免费pdf转word免费的软件:https://www.woniuppt.com/jszhuanhua/217.html
免费word怎么转pdf:https://www.woniuppt.com/jszhuanhua/216.html
免费word转换pdf:https://www.woniuppt.com/htmlzhuanhua/215.html
免费pdf转换:https://www.woniuppt.com/htmlzhuanhua/214.html
免费在线pdf转word:https://www.woniuppt.com/htmlzhuanhua/213.html
免费pdf压缩:https://www.woniuppt.com/pngzhuanhua/212.html
免费jpg转pdf:https://www.woniuppt.com/pngzhuanhua/211.html
免费pdf合并:https://www.woniuppt.com/pngzhuanhua/210.html
免费word怎么转pdf:https://www.woniuppt.com/pngzhuanhua/209.html
免费pdf编辑器:https://www.woniuppt.com/jpgzhuanhua/208.html
免费excel转pdf:https://www.woniuppt.com/jpgzhuanhua/207.html
免费excel转pdf:https://www.woniuppt.com/jpgzhuanhua/206.html
免费pdf压缩:https://www.woniuppt.com/jpgzhuanhua/205.html
免费word怎么转pdf:https://www.woniuppt.com/jpgzhuanhua/204.html
免费pdf怎么转换成word:https://www.woniuppt.com/jpgzhuanhua/203.html
免费pdf转word免费的软件:https://www.woniuppt.com/pdfzhuanhua/202.html
免费怎么将pdf转换成word:https://www.woniuppt.com/pdfzhuanhua/201.html
免费pdf转换成excel:https://www.woniuppt.com/pdfzhuanhua/200.html
免费pdf怎么转换成jpg图片:https://www.woniuppt.com/pdfzhuanhua/199.html
免费pdf转换:https://www.woniuppt.com/pdfzhuanhua/198.html
免费pdf转换器:https://www.woniuppt.com/qitagongju/185.html
免费怎么把pdf文件转换成word:https://www.woniuppt.com/qitagongju/184.html
免费pdf怎么转换成word:https://www.woniuppt.com/qitagongju/183.html
免费excel转pdf:https://www.woniuppt.com/hebingpdf163/182.html
免费jpg转pdf:https://www.woniuppt.com/hebingpdf163/181.html
免费pdf阅读器:https://www.woniuppt.com/hebingpdf163/180.html
免费pdf转换器:https://www.woniuppt.com/pdfanquan/179.html
免费在线pdf转word:https://www.woniuppt.com/pdfanquan/178.html
免费pdf转excel:https://www.woniuppt.com/pdfanquan/177.html
免费excel转pdf:https://www.woniuppt.com/pdfanquan/176.html
免费图片转pdf:https://www.woniuppt.com/chakanhebianji/175.html
免费pdf转word:https://www.woniuppt.com/chakanhebianji/174.html
免费pdf转图片:https://www.woniuppt.com/chakanhebianji/173.html
免费excel转pdf:https://www.woniuppt.com/chakanhebianji/172.html
免费pdf转换成excel:https://www.woniuppt.com/chakanhebianji/171.html
免费pdf转word免费的软件:https://www.woniuppt.com/chakanhebianji/170.html
免费pdf转jpg:https://www.woniuppt.com/pdfzhuanhuan/169.html
免费pdf合并:https://www.woniuppt.com/pdfzhuanhuan/168.html
免费word转换pdf:https://www.woniuppt.com/pdfzhuanhuan/167.html
免费pdf转ppt:https://www.woniuppt.com/pdfzhuanhuan/166.html
免费合并pdf:https://www.woniuppt.com/pdfzhuanhuan/165.html
免费pdf阅读器:https://www.woniuppt.com/pdfzhuanhuan/164.html
免费pdf怎么编辑修改内容:https://www.woniuppt.com/congpdfzhuanhuanwendang/163.html
免费合并pdf:https://www.woniuppt.com/congpdfzhuanhuanwendang/162.html
免费pdf转word免费的软件:https://www.woniuppt.com/congpdfzhuanhuanwendang/161.html
免费jpg转pdf:https://www.woniuppt.com/congpdfzhuanhuanwendang/160.html
免费pdf编辑软件:https://www.woniuppt.com/congpdfzhuanhuanwendang/159.html
【如何使用Python实现一个pdf阅读器?】免费word怎么转pdf:https://www.woniuppt.com/congpdfzhuanhuanwendang/158.html

相关经验推荐