gpt4 book ai didi

python-3.x - "PDF File has not been decrypted"问题在 PyPDF2 中仍然存在

转载 作者:行者123 更新时间:2023-12-04 01:47:32 35 4
gpt4 key购买 nike

使用PyPDF2阅读PDF文件时出现以下错误

raise utils.PdfReadError("File has not been decrypted")
PdfReadError: File has not been decrypted

我一直在尝试通过python以编程方式阅读PDF文档。对于大多数 PDF 文件,它工作正常,但对于少数文件,我收到以下错误

raise utils.PdfReadError("File has not been decrypted")
PdfReadError: File has not been decrypted

我已经尝试过另一个 stackoverflow 解决方案的解决方案:PyPDF 2 Decrypt Not Working

上述问题的解决方案仍然没有解决我的问题

import os
import PyPDF2
from PyPDF2 import PdfFileReader

fp = open(filename)
pdfFile = PdfFileReader(fp)
if pdfFile.isEncrypted:
try:
pdfFile.decrypt('')
print('File Decrypted (PyPDF2)')
except:
command = ("cp "+ filename +
" temp.pdf; qpdf --password='' --decrypt temp.pdf " + filename
+ "; rm temp.pdf")
os.system(command)
print('File Decrypted (qpdf)')
fp = open(filename)
pdfFile = PdfFileReader(fp)
else:
print('File Not Encrypted')

问题似乎不是文件名之间的空格或将密码设置为 ' '。

不知何故无法解决这个错误。任何帮助表示赞赏。谢谢。

我的代码:

import PyPDF2
import os
from os import listdir
from os.path import isfile, join

mypath='D:/POC PDF'
onlyfiles = [os.path.join(mypath, f) for f in os.listdir(mypath) if os.path.isfile(os.path.join(mypath, f))]
for file in onlyfiles:
fileReader = PyPDF2.PdfFileReader(open(file,'rb'))
countpage = fileReader.getNumPages()
print(countpage)

最佳答案

回答我自己的问题:感谢我的一个 friend ,我找到了一个比 PyPDF2 更好的包。它是 PyMuPDF。这是一个示例实现

import fitz

def extractText(file):
doc = fitz.open(file)
text = []
for page in doc:
t = page.getText().encode("utf8")
text.append(t)
return text

关于python-3.x - "PDF File has not been decrypted"问题在 PyPDF2 中仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54650830/

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