无法弄清楚如何关闭打开的引用

Can't figure out how to close an open reference

基本上我是从 PDF 中提取页面,将图形文本粘贴到提取的页面中,然后将提取的页面保存到同一文件夹中。我已经让一切都按预期工作,但我最终得到了我无法弄清楚如何关闭的预编辑文件。我需要删除的文件将始终是 'delete.pdf'...但是当我尝试在代码末尾删除它时,它说它当前正在使用中。我不知道如何关闭对正在编辑的 'DELETE.pdf' 文件的最后引用。任何帮助将不胜感激,我是 Python.

的新手

我认为这是由于这个打开 (existing_pdf = PdfFileReader(open("DELETE.pdf", "rb")))

from PyPDF2 import PdfFileReader, PdfFileWriter
import os 
import ntpath
import pdfplumber
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.colors import HexColor

print('Paste the location of the file')
location = input()

plan = ntpath.basename(location)
os.chdir(location)
pdf = pdfplumber.open(plan +'.pdf')

page = pdf.pages[4]
text = page.extract_text()

ending = text.split("ft. or",1)[1]  
minimum = str(ending.split()[:1])

min2 = minimum[2:]
min3 = min2[:2]
Wording = (str(min3) + ' inches') 
pdf.close()

pdf_file_path = 'Plan.pdf'
file_base_name = pdf_file_path.replace('.pdf', '')
 
pdf = PdfFileReader(pdf_file_path)
 
pages = [1] 
pdfWriter = PdfFileWriter()
 
for page_num in pages:
    pdfWriter.addPage(pdf.getPage(page_num))
 
with open('DELETE.pdf'.format(file_base_name), 'wb') as f:
    pdfWriter.write(f)
    f.close()

packet = io.BytesIO()

can = canvas.Canvas(packet, pagesize=letter)
can.setFillColor(HexColor(0xFF0000)) #sets color to red
can.setFont("Helvetica", 8) #sets font
can.drawString(75, 120, Wording) #inserts the min height
can.save()

#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)

# read your existing PDF
**existing_pdf = PdfFileReader(open("DELETE.pdf", "rb"))**
output = PdfFileWriter()

# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)

# finally, write "output" to a real file
with open('{0} New File.pdf'.format(file_base_name), "wb") as f:
    output.write(f)
    f.close()

正如你所说,你没有关闭打开的文件。 您无法在此处进行更改,而只是在对文件使用 PdfFileReader 之前获取对该文件的引用。 第一个选项:

# read your existing PDF
read_pdf_file_handle = open("DELETE.pdf", "rb")
existing_pdf = PdfFileReader(read_pdf_file_handle)
output = PdfFileWriter()

# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)

# close the reference to it
read_pdf_file_handle.close()

# finally, write "output" to a real file
with open('{0} New File.pdf'.format(file_base_name), "wb") as f:
    output.write(f)
    # f.close() - unnecessary, file is closed on exiting the with statement

第二个选项:

# read your existing PDF
with open("DELETE.pdf", "rb") as read_pdf:
    existing_pdf = PdfFileReader(read_pdf)
    page = existing_pdf.getPage(0)

output = PdfFileWriter()

# add the "watermark" (which is the new pdf) on the existing page
page.mergePage(new_pdf.getPage(0))
output.addPage(page)

# finally, write "output" to a real file
with open('{0} New File.pdf'.format(file_base_name), "wb") as f:
    output.write(f)
    # f.close() - unnecessary, file is closed on exiting the with statement

如果您 运行 需要在 with 中打开 pdf 的整个代码块,则无需关闭它。 这是 PdfPlumber GitHub docs

中的示例
with pdfplumber.open("path/to/file.pdf") as pdf:
    first_page = pdf.pages[0]
    print(first_page.chars[0])