我试图用PyPDF2编写一个python程序来拆分PDF。因此,我必须定义一个列表来附加元素,但当循环再次迭代时,列表的值将更改为空白列表。因此,该程序将生成一个pdf,其中没有任何内容。这是我的密码
import tkinter as tk
from PIL import Image, ImageTk
from PyPDF2 import PdfFileReader, PdfFileWriter
from tkinter import filedialog
from tkinter import messagebox
elements = []
root7 = tk.Tk()
root7.geometry("600x600")
root7.title("PDF Viewer")
root7.configure(bg="#202020")
image7 = Image.open("split.jpg")
new_img7 = image7.resize((600,400))
photo7 = ImageTk.PhotoImage(new_img7)
def open1():
pdf = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("pdf files","*.pdf"),("all files","*.*")))
elements.append(pdf)
w = tk.Spinbox(root7, from_=0, to=100, width=3)
w.place(x=150, y=510)
elements.append(int(w.get()))
w1 = tk.Spinbox(root7, from_=0, to=100, width=3)
w1.place(x=409, y=510)
elements.append(int(w1.get()))
def split():
pdfs = PdfFileReader(elements[0], "rb")
pdf_writer = PdfFileWriter()
for page in range(elements[1], elements[2]):
pdf_writer.addPage(pdfs.getPage(page))
output_fname = "splitted.pdf"
with open(output_fname, 'wb') as out:
pdf_writer.write(out)
messagebox.showinfo("Splitted","PDF Saved in Desktop as splitted.pdf")
lbl7 = tk.Label(root7,image=photo7)
lbl7.place(x=0, y=0)
button7 = tk.Button(root7, text= 'Open PDF', command=open1, bg="#B8B8B8")
button7.place(width=300, height=50, x=150, y=450)
button8 = tk.Button(root7, text= 'Split PDF', command=split, bg="#B8B8B8")
button8.place(width=300, height=50, x=150, y=540)
root7.update()
root7.mainloop()
有人请帮忙
我终于找到了答案。问题不在于列表,而是在我获取spinbox小部件的初始值时引起的,下面是修改后的代码
GUI不太好,但仍然可以工作
相关问题 更多 >
编程相关推荐