gpt4 book ai didi

python - 在 Python 中解压 PDF 中的 FlateDecode 对象

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

我正在尝试使用以下代码解压 PDF 格式的数据

import re
import zlib

pdf = open("some_doc.pdf", "rb").read()
stream = re.compile(r'.*?FlateDecode.*?stream(.*?)endstream', re.S)

for s in stream.findall(pdf):
s = s.strip('\r\n')
try:
print(zlib.decompress(s))
print("")
except:
pass

但它告诉我以下错误 文件“D:\pdf_flatedecode.py”,第 8 行,位于 对于 stream.findall(pdf) 中的 s:类型错误:不能在类似字节的对象上使用字符串模式请帮我。我无法找出问题所在。我的python版本是3.7.1

最佳答案

核心问题是您以“二进制”模式打开 pdf,因此您必须从字节而不是 str 编译正则表达式。我不确定 100% 它是否按您预期的方式工作,但试试这个:

import re
import zlib

pdf = open("some_doc.pdf", "rb").read()
stream = re.compile(b'.*?FlateDecode.*?stream(.*?)endstream', re.S)

for s in re.findall(stream,pdf):
s = s.strip(b'\r\n')
try:
print(zlib.decompress(s).decode('UTF-8'))
print("")
except:
pass

关于python - 在 Python 中解压 PDF 中的 FlateDecode 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53608910/

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