Your Cart
Loading
How to Close a PDF Document in Python

How to Close Your PDF with doc.close() in PyMuPDF

doc.close() closes a PDF document that you previously opened with PyMuPDF.


What does doc.close() do?

doc.close():

  • Releases resources and memory associated with the document.
  • Closes the document and its underlying resources.
  • Makes the Document object unavailable for further document operations.


When should you use it?

Use doc.close() when you are finished working with a PDF, especially when:

  • You created or modified a PDF.
  • You are processing many PDFs in a loop.
  • You want to make sure the document is no longer being held open.


Important: After calling doc.close(), you can no longer work with the document. Attempting to perform operations on a closed document can raise: ValueError: document closed


If you need to work with the PDF again, you can open it with pymupdf.open():

How to Open a PDF Document with PyMuPDF


This creates a new Document object that you can work with normally.


Should you save the PDF document before closing it?

If you've modified the PDF, make sure you save your changes to a new file before closing the document.


For example:

How to Save a Modified PDF


If you close a modified document without saving it, your changes may be lost.


doc.close() does not save your changes.



Open the notebook

Open the Google Colab notebook for this PDF mini-guide and run the code as you follow along.

[Open in Google Colab]




If you found this mini-guide useful, continue with our interactive beginner cheatsheet and explore more Python PDF foundations.


👉 Download our free Python PDF Foundations Cheatsheet→





More PDF Python Resources

Want to learn how to open a PDF file?


Check out this mini-guide: Open Your First PDF with PyMuPDF.



Subscribe to our newsletter

Get notified when new PDF mini-guides, resources, templates, bundles, ebooks, and courses are released.