gpt4 book ai didi

python - 从 pdf 文件创建 .txt 文件

转载 作者:行者123 更新时间:2023-11-28 22:51:13 24 4
gpt4 key购买 nike

现在我正在用 python 编写一个程序,它需要你打开某个 .pdf 文件,control+A(全选),control C 和 control V(复制和粘贴)到一个 .txt 文件上,然后运行程序。

我想知道是否有任何方法可以跳过一个步骤并运行程序,而无需执行这一系列步骤,只需引用程序中的 pdf 文件。

类似于:

##does the procedure above and saves it on a notes.txt file##
FILE_NAME = 'notes.pdf'
read_pdf(FILE_NAME,'notes.txt')

最佳答案

使用slate模块,它取决于 pdfminer .

安装它:

pip install pdfminer==20131113
pip install https://codeload.github.com/timClicks/slate/zip/master

使用方法:

import slate

with open('example.pdf') as fp:
doc = slate.PDF(fp)

print(len(doc))
print(doc[0])

4
This is a test.

注意事项:

  • pdfminer 模块没有 support Python 3

  • 你需要从主仓库安装slate,因为pypi slate 版本很旧,不是compatiblelast changepdfminer

或者使用PyPDF2 :

安装它:

pip install PyPDF2

使用方法:

import PyPDF2

pdf = PyPDF2.PdfFileReader(open('sample.pdf', "rb"))

print(pdf.getNumPages())
print(pdf.getPage(0).extractText())

1
This is a sample.

关于python - 从 pdf 文件创建 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com