I'm working on a Python script that uses the PyMuPDF library to modify a PDF document and then save the modified content to a temporary PDF file. However, I'm encountering a "Permission denied" error when trying to write the content of the pdf file the temporary file. I've looked into various solutions, but I'm still stuck. Here's the simplified example of my code:
from tempfile import NamedTemporaryFile
import fitz
doc = fitz.open("test.pdf")
with NamedTemporaryFile(suffix=".pdf", delete=False) as temp_file:
temp_pdf_path = temp_file.name
doc.save(temp_pdf_path)
doc.close()
The error is:
Traceback (most recent call last): File "C:\Users\user\PycharmProjects\test\test.py", line 8, in doc.save(temp_pdf_path) File "C:\Users\user\PycharmProjects\test\venv\lib\site-packages\fitz\fitz.py", line 4629, in save return _fitz.Document_save(self, filename, garbage, clean, deflate, deflate_images, deflate_fonts, incremental, ascii, expand, linear, no_new_id, appearance, pretty, encryption, permissions, owner_pw, user_pw) RuntimeError: cannot remove file 'C:\Users\user\AppData\Local\Temp\tmp1ivvrwvj.pdf': Permission denied