I recently wrote a command line script that takes a range of pages from a pdf and makes a new pdf out of them. I am now trying to turn that script into a small gui program.
I am having problems getting the slice method code to run when the button is clicked however. As of now running the program bring up the gui, but when I click the button I get this error message in the shell:
Traceback (most recent call last):
File "pdfSliceGui.py", line 54, in slice
old = oldPDF.get_text()
NameError: global name 'oldPDF' is not defined
There seems to be a problem with the slice() method not being able to read what is in the Entries. And it appears to be a scope problem, the slice() method doesn't recognize the entry. There may also be an issue with me converting some of the entries to Ints but my primary focus is getting the slice() method to work with the Entry data.
Here is the relevant part of the code I wrote:
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="PDF Slicer")
self.set_size_request(320, 320)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
self.add(vbox)
label1 = Gtk.Label()
label1.set_text("PDF to extract from")
vbox.pack_start(label1, True, True, 0)
oldPDF = Gtk.Entry()
vbox.pack_start(oldPDF, True, True, 0)
label2 = Gtk.Label()
label2.set_text("PDF to extract to")
vbox.pack_start(label2, True, True, 0)
newPDF = Gtk.Entry()
vbox.pack_start(newPDF, True, True, 0)
label3 = Gtk.Label()
label3.set_text("Start page")
vbox.pack_start(label3, True, True, 0)
start = Gtk.Entry()
vbox.pack_start(start, True, True, 0)
label4 = Gtk.Label()
label4.set_text("End page")
vbox.pack_start(label4, True, True, 0)
end = Gtk.Entry()
vbox.pack_start(end, True, True, 0)
button = Gtk.Button.new_with_label("Slice")
button.connect("clicked", self.slice)
vbox.pack_start(button, True, True, 0)
def slice(self, button):
old = oldPDF.get_text()
new = newPDF.get_text()
s = int(start.get_text()) - 1
e = int(end.get_text())
pdfFileObj = open(old, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pdfWriter = PyPDF2.PdfFileWriter()
for page in range (s, e):
pageObj = pdfReader.getPage(page)
pdfWriter.addPage(pageObj)
pdfOutputFile = open(new, 'wb')
pdfWriter.write(pdfOutputFile)
pdfOutputFile.close()
pdfFileObj.close()