2 つの PDF をマージする簡単なスクリプトを作成しようとしていますが、出力をディスクに保存しようとすると問題が発生します。私のコードは
from PyPDF2 import PdfFileWriter, PdfFileReader
import tkinter as tk
from tkinter import filedialog
### Prompt the user for the 2 files to use via GUI ###
root = tk.Tk()
root.update()
file_path1 = tk.filedialog.askopenfilename(
filetypes=[("PDF files", "*.pdf")],
)
file_path2 = tk.filedialog.askopenfilename(
filetypes=[("PDF files", "*.pdf")],
)
###Function to combine PDFs###
output = PdfFileWriter()
def append_pdf_2_output(file_handler):
for page in range(file_handler.numPages):
output.addPage(file_handler.getPage(page))
#Actually combine the 2 PDFs###
append_pdf_2_output(PdfFileReader(open(file_path1, "rb")))
append_pdf_2_output(PdfFileReader(open(file_path2, "rb")))
###Prompt the user for the file save###
output_name = tk.filedialog.asksaveasfile(
defaultextension='pdf')
###Write the output to disk###
output.write(output_name)
output.close
問題は、次のエラーが発生することです
UserWarning: 書き込み先のファイルはバイナリ モードではありません。正しく書き込まれていない可能性があります。[pdf.py:453] トレースバック (最新の呼び出しが最後): ファイル "Combine2Pdfs.py"、44 行目、output.write(output_name) ファイル "/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho n3.5/site-packages/P yPDF2/pdf.py"、487 行目、書き込み stream.write(self. header + b ("\n")) の TypeError: write() 引数は str である必要があります。バイト
どこで間違ったのですか?