如何将pyPDF2的输出保存到Excel文件中?

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

以下代码打印我需要的内容(格式不理想,但如果我能找到如何另存为 Excel 文件,这可能无关紧要)。

for i in range(3,167):
    print(reader.pages[i].extract_text().split('\n'))

我尝试使用 Pandas 来保存输出:

for i in range(3,167):
    (reader.pages[i].extract_text().split('\n')).to_excel('output.xlsx', index = False)

我不精通Python。如果有更好的方法来做到这一点,请告诉我。我不太明白如何很好地使用 Camelot。

python pandas pdf extract pypdf
1个回答
0
投票

尝试从 pdf 中解析您需要的内容,然后保存数据框

import pandas as pd

#store the data
data = []

for i in range(3, 167):
    text = reader.pages[i].extract_text()
    lines = text.split('\n')
    data.extend(lines)

# DataFrame from the list
df = pd.DataFrame(data, columns=['Text'])

# Save it to an Excel file
df.to_excel('output.xlsx', index=False)
© www.soinside.com 2019 - 2024. All rights reserved.