我正在尝试编写一些工作脚本,而我在研究某个特定问题时遇到了困难。我假设每个PDF页面都是一个图像,例如jpg,但即使我正在阅读文件,它也不会发生这种情况。所以我的问题是:存储的各个PDF页面是什么,好像不是图像?
以下是我工作的代码:
pdf = user_file.file.read()
startmark = b"\xff\xd8"
startfix = 0
endmark = b"\xff\xd9"
endfix = 2
i = 0
njpg = 0
while True:
istream = pdf.find("stream", i)
if istream < 0:
break
istart = pdf.find(startmark, istream, istream+20)
if istart < 0:
i = istream+20
continue
iend = pdf.find("endstream", istart)
if iend < 0:
raise Exception("Didn't find end of stream!")
iend = pdf.find(endmark, iend-20)
if iend < 0:
raise Exception("Didn't find end of JPG!")
istart += startfix
iend += endfix
print "JPG %d from %d to %d" % (njpg, istart, iend)