问题描述
当前,我正在使用PyPDF 2作为依赖项.
Currently I am using the PyPDF 2 as a dependency.
我遇到了一些加密文件并已处理像往常一样(在下面的代码中):
I have encountered some encrypted files and handledthem as you normally would (in the following code):
PDF = PdfFileReader(file(pdf_filepath, 'rb'))
if PDF.isEncrypted:
PDF.decrypt("")
print PDF.getNumPages()
我的文件路径看起来像〜/blah/FDJKL492019 21490,LFS.pdf"PDF.decrypt(")返回1,表示成功.但是当它到达print PDF.getNumPages()时,仍然会引发错误"PyPDF2.utils.PdfReadError:文件尚未解密".
My filepath looks something like "~/blah/FDJKL492019 21490 ,LFS.pdf"PDF.decrypt("") returns 1, which means it was successful. But when it hits print PDF.getNumPages(),it still raises the error, "PyPDF2.utils.PdfReadError: File has not been decrypted".
如何摆脱这个错误?我可以通过双击打开PDF文件(默认情况是使用Adobe Reader打开).
How do I get rid of this error?I can open the PDF file just fine by double click (which default-opens with Adobe Reader).
推荐答案
回答我自己的问题:如果文件名中包含空格,那么即使返回成功代码,PyPDF 2解密功能最终也会失败.通过PyPDF2运行PDF之前,请在命名PDF时尽量使用下划线.
To Answer My Own Question:If you have ANY spaces in your file name, then PyPDF 2 decrypt function will ultimately fail despite returning a success code.Try to stick to underscores when naming your PDFs before you run them through PyPDF2.
例如,
执行"FDJKL492019_21490_,LFS.pdf"之类的操作,而不是"FDJKL492019 21490,LFS.pdf".
Rather than "FDJKL492019 21490 ,LFS.pdf" do something like "FDJKL492019_21490_,LFS.pdf".
这篇关于PyPDF 2解密不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!