Most forward way to copy a whole Document ? #2569
-
My script is loading a long pdf, splitting it into chapters and analyzing each chapter. Somehow I noticed: it's not that "straight forward" to copy a whole in-memory pymupdf document to a new in memory object. Using copy's module I ended up with this workaround to copy the doc in memory
Note 1: Note 2: Final questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think I don't completely understand what you are actually trying to do. Just analyzing the PDF's content - no update intent? If this is true, you can do whatever you want with the document: inserting notes, images, ... whatever. Otherwise, you can open the same file two times, which creates different |
Beta Was this translation helpful? Give feedback.
-
The functions But there is an easy way to make memory copies of the current state of the document like this: pdfdata = doc.tobytes(<save options>)
temp = fitz.open("pdf", pdfdata) The document That temporary document can be saved to disk like normal whenever deemed required. You can create several |
Beta Was this translation helpful? Give feedback.
The functions
copy()
/deepcopy()
of Python modulecopy
are for pure Python objecs only. Most PyMuPDF objects are far more than that: they have a companion or "shadow" objects within the base library MuPDF.Therefore these functions cannot be used for what you want.
But there is an easy way to make memory copies of the current state of the document like this:
The document
temp
is a full copy ofdoc
. "Save options" means parameters you would also use when saving the document, likegarbage
,deflate
etc.That temporary document can be saved to disk like normal whenever deemed required.
You can create several
temp
objects fr…