![](/img/trans.png)
[英]Migrate anaconda from python v3.6 to v3.7 and preserve all conda and pip packages
[英]Cannot merge pdf in python v3.6
我有以下經過測試可在python ver2.7中工作的代碼段,該代碼將多個pdf合並為一個pdf。
from PyPDF2 import PdfFileMerger, PdfFileReader
#merge individual pdfs of each page into a single pdf
merger = PdfFileMerger()
for filename in pdf_list:
merger.append(PdfFileReader(file("./" + pdf_location + "/" + filename, 'rb')))
當我在python v3.6中運行相同的代碼時,它失敗並顯示以下錯誤。
NameError: name 'file' is not defined
如何修改代碼以使其在python v3.6中工作?
我以前沒有使用過PdfFileReader,但是從文檔中來看,它需要一個文件流作為參數。 因此,嘗試將“文件”更改為“打開”,該文件流應將指向讀取二進制模式中該位置的文件流傳遞給PdfFileReader構造函數。 因此,您的附加行應如下所示:
merger.append(PdfFileReader(open("./" + pdf_location + "/" + filename, 'rb')))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.