尝试从PDF提取文本数据失败

问题描述 投票:0回答:1

我正在尝试编写一小段Python代码,以将数据从UK Google Community Mobility Reports提取到CSV文件。

为此,我正在使用此代码:

import PyPDF2

FILE_PATH = '....2020-03-29_GB_Mobility_Report_en.pdf'
file = open(FILE_PATH, 'rb')
fileReader = PyPDF2.PdfFileReader(file)

for each in range(fileReader.numPages):
     print(fileReader.getPage(each).extractText())

但是,当我尝试打印每页的内容时,它不会打印任何文本。该代码将打开正确的文件,因为它提供了正确的页数。为什么会这样,我该如何解决?

python python-3.x pdf pypdf2
1个回答
0
投票

我无法使用PyPDF2提取文本,但可以使用tika提取文本。

from tika import parser

parsedPDF = parser.from_file('2020-03-29_GB_Mobility_Report_en.pdf')
pdf = parsedPDF["content"]
pdf = pdf.replace('\n\n', '\n')
print(pdf)

这是我之前在PDF提取中写的:Python Data Extraction from an Encrypted PDF

© www.soinside.com 2019 - 2024. All rights reserved.