두 개의 PDF를 병합하는 간단한 스크립트를 작성하려고하지만 출력을 디스크에 저장하려고 할 때 문제가 발생했습니다. 바이너리 모드가 아닌 쓸 파일 : 내 코드는 문제는 내가pypdf2로 pdf를 작성하면 오류가 발생합니다.
UserWarning의 오류가 있다는 것입니다
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
입니다. 올바르게 기록되지 않을 수 있습니다. [pdf.py:453] Traceback (최근 호출 마지막) : 44 행의 output.write (output_name) 파일에서 "Combine2Pdfs.py"파일 "/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho ("\ n")) TypeError : write() 인수는 str이어야합니다. (예 : \ n3.5/site-packages/P yPDF2/pdf.py ", 487 줄, write stream.write (self. header + b , 바이트가 아님
어디서 잘못 되었나요?
메시지뿐 아니라 전체 스택 트레이스를 게시 할 수 있습니까? –
주석에 스택 추적을 게시하지 마십시오. 질문에 "편집"링크가있어 질문을 수정할 수 있습니다. –