繁体   English   中英

将PDF转换/写入为RAM,就像文件一样,以便进一步使用它

[英]Convert/Write PDF to RAM as file-like object for further working with it

我的脚本生成PDF( PyPDF2.pdf.PdfFileWriter object )并将其存储在变量中。 我需要在脚本中进一步将其作为类似于file-like object 但是现在我必须先将其写入HDD。 然后,我必须将其打开为文件才能使用。

为了防止这种不必要的写/读操作,我发现了许多解决方案StringIOBytesIO等。 但是我找不到在我的情况下能真正帮助我的东西。

据我了解-我需要将“ PyPDF2.pdf.PdfFileWriter object “转换”(或写入RAM)以直接使用file-like object

还是有另一种方法完全适合我的情况?

更新-这是代码示例

from pdfrw import PdfReader, PdfWriter, PageMerge
from PyPDF2 import PdfFileReader, PdfFileWriter


red_file = PdfFileReader(open("file_name.pdf", 'rb'))

large_pages_indexes = [1, 7, 9]

large = PdfFileWriter()
for i in large_pages_indexes:
    p = red_file.getPage(i)
    large.addPage(p)

# here final data have to be written (I would like to avoid that)
with open("virtual_file.pdf", 'wb') as tmp:
  large.write(tmp)

# here I need to read exported "virtual_file.pdf" (I would like to avoid that too)
with open("virtual_file.pdf", 'rb') as tmp:
  pdf = PdfReader(tmp) # here I'm starting to work with this file using another module "pdfrw"
  print(pdf)

为避免磁盘I / O变慢,您似乎要更换

with open("virtual_file.pdf", 'wb') as tmp:
  large.write(tmp)

with open("virtual_file.pdf", 'rb') as tmp:
  pdf = PdfReader(tmp)

buf = io.BytesIO()
large.write(buf)
buf.seek(0)
pdf = PdfReader(buf)

另外,您可以使用buf.getvalue()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM