gpt4 book ai didi

python - 如何在 Python 中解锁 "secured"(读保护)PDF?

转载 作者:太空狗 更新时间:2023-10-29 17:39:38 25 4
gpt4 key购买 nike

在 Python 中我使用 pdfminer使用此消息下方的代码从 pdf 中阅读文本。我现在收到一条错误消息:

File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdfpage.py", line 124, in get_pages
raise PDFTextExtractionNotAllowed('Text extraction is not allowed: %r' % fp)
PDFTextExtractionNotAllowed: Text extraction is not allowed: <cStringIO.StringO object at 0x7f79137a1
ab0>

当我使用 Acrobat Pro 打开此 pdf 时,发现它是安全的(或“读保护”)。来自 this link然而,我读到有许多服务可以轻松禁用此读取保护(例如 pdfunlock.com 。当深入研究 pdfminer 的源代码时,我看到上面的错误是在 these lines 上生成的。

if check_extractable and not doc.is_extractable:
raise PDFTextExtractionNotAllowed('Text extraction is not allowed: %r' % fp)

由于有许多服务可以在一秒钟内禁用此读保护,我认为这很容易做到。 .is_extractable 似乎是doc 的一个简单属性,但我认为这不是将.is_extractable 更改为True 这么简单..

有人知道如何使用 Python 禁用 pdf 的读取保护吗?欢迎所有提示!

============================================= =

您将在下面找到我当前用于从非读保护中提取文本的代码。

def getTextFromPDF(rawFile):
resourceManager = PDFResourceManager(caching=True)
outfp = StringIO()
device = TextConverter(resourceManager, outfp, codec='utf-8', laparams=LAParams(), imagewriter=None)
interpreter = PDFPageInterpreter(resourceManager, device)

fileData = StringIO()
fileData.write(rawFile)
for page in PDFPage.get_pages(fileData, set(), maxpages=0, caching=True, check_extractable=True):
interpreter.process_page(page)
fileData.close()
device.close()

result = outfp.getvalue()

outfp.close()
return result

最佳答案

我在尝试让 qpdf 在我的程序中运行时遇到了一些问题。我找到了一个有用的库,pikepdf ,它基于 qpdf 并自动将 pdf 转换为可提取的。

使用它的代码非常简单:

import pikepdf

pdf = pikepdf.open('unextractable.pdf')
pdf.save('extractable.pdf')

关于python - 如何在 Python 中解锁 "secured"(读保护)PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28192977/

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