[英]PyPDF2 Encrypting PDF with Python on Windows: AttributeError: 'tuple' object has no attribute 'write'
[英]Python PyPDF2 'PdfFileReader' object has no attribute 'scaleTo' error
這是我的第一個程序,所以我想有很多效率低下的地方。 首先,我創建了一個適用於組合 PDF 的 GUI。在嘗試將工作代碼轉換為循環訪問多個單頁 PDF 目錄的代碼時,我收到錯誤消息。 在“PageObj.scaleTo(11 72, 17 72)”行中,問題標題出現錯誤。 GUI 將用戶輸入用於變量“x”(目錄)、“a”(紙張大小)和“s”(狀態)。 它是將頁面調整為選定的大小,與模板合並(不是 append,而是單頁“PDF 三明治”,我聽說過它的描述),然后覆蓋現有文件。 這將發生在指定目錄中的每個 PDF 上。 我已經嘗試了幾個版本來定義我的 PageObj 變量,但似乎無法正確定義。
# Variables for User input values
x = values["-pdf_dir-"]
a = values["-paper_size-"]
s = values["-state-"]
# Location to find seal templates
state = f"G:/Drafting/Kain Mincey/Allen's seals/Correctly Sized/{a}/{s}.pdf"
Seal_pdf = PdfFileReader(open(state, "rb"), strict=False)
input_pdf = glob.glob(os.path.join(x, '*.pdf'))
output_pdf = PdfFileWriter()
page_count = len(fnmatch.filter(os.listdir(x), '*.pdf'))
i = 0
if a == "11x17":
for file in input_pdf:
sg.OneLineProgressMeter('My Meter', i, page_count, 'And now we Wait.....')
PageObj = PyPDF2.PdfFileReader(open(file, "rb"))
PageObj.scaleTo(11*72, 17*72)
PageObj.mergePage(Seal_pdf.getPage(0))
output_pdf.addPage(PageObj)
output_filename = f"{x[:-4]}"
i = i + 1
PdfFileReader 返回整個文件。 scaleTo 適用於頁面。 您必須使用 getPage 獲取您想要的頁面。 –蒂姆·羅伯茨 3 月 28 日 21:02
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.