簡體   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