私は多くのPDFを開いていますが、解析した後にPDFを削除したいのですが、プログラムが実行されるまでファイルは開いたままです。 PyPDF2を使用して開かれたPDfを閉じるにはどうすればよいですか?PdfFileReaderファイルを閉じる方法はありますか?
コード:
def getPDFContent(path):
content = ""
# Load PDF into pyPDF
pdf = PyPDF2.PdfFileReader(file(path, "rb"))
#Check for number of pages, prevents out of bounds errors
max = 0
if pdf.numPages > 3:
max = 3
else:
max = (pdf.numPages - 1)
# Iterate pages
for i in range(0, max):
# Extract text from page and add to content
content += pdf.getPage(i).extractText() + "\n"
# Collapse whitespace
content = " ".join(content.replace(u"\xa0", " ").strip().split())
#pdf.close()
return content
。ありがとう! – SPYBUG96