0

Been using Python for a very short amount of time and can't figure out what is wrong with this code. I can't find any examples that would work for my code, so I'm asking here.

import sys
import os
from PyPDF2 import PdfFileReader, PdfFileMerger, PdfFileWriter
from tkinter import *

gui = Tk()
gui.resizable(0,0)

gui.geometry("800x600")
gui.title("PDF Tools")

l = StringVar()
l2= StringVar()
f = StringVar()
e = StringVar()

filenamelabel = Label(textvariable=l).place(x=400, y=0)
filenamelabel2 = Label(textvariable=l2).place(x=400, y=25)
exportfolderlabel = Label(textvariable=f).place(x=400, y=50)

finalFileNameForm = Entry(gui).place(x=0, y=75)

def openFileDialog():
    global fileName
    fileName = filedialog.askopenfilename(initialdir = "/Users/", title="Pick a PDF           file.", filetypes=(("pdf files","*.pdf"),("all files","*.*")))
    l.set(fileName)

def openFileDialog2():
    global fileName2
    fileName2 = filedialog.askopenfilename(initialdir = "/Users/", title="Pick a second     PDF file.", filetypes=(("pdf files","*.pdf"),("all files","*.*")))
    l2.set(fileName2)

def openExportFolderDialog():
    global exportFolder
    exportFolder = filedialog.askdirectory(initialdir = "/Users/", title="Pick an export folder.")
    f.set(exportFolder)

def append_pdf(input, output):
    [output.addPage(input.getPage(page_num)) for page_num in range(input.numPages)]

def combinePdf():
    output = PdfFileWriter()
    append_pdf(PdfFileReader(file(fileName, "rb")),output)
    append_pdf(PdfFileReader(file(fileName2, "rb")),output)

    output.write(file(finalFileName, exportFolder))

importpdf1 = Button(text="Import PDF", command=openFileDialog).place(x=0, y=0)
importpdf2 = Button(text="Import PDF 2", command=openFileDialog2).place(x=0, y=25)
setexport = Button(text="Set Export Folder", command=openExportFolderDialog).place(x=0, y=50)
combinepdf = Button(text="Combine PDFs", command=combinePdf).place(x=0, y=100)

gui.mainloop()

The error I am getting when I run the code is

Exception in Tkinter callback Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tkinter/init.py", line 1487, in call return self.func(*args) File "/Users/chris/Desktop/gui.py", line 43, in combinePdf append_pdf(PdfFileReader(file(fileName, "rb")),output) NameError: name 'file' is not defined

Using Python 3.4.1, OS X Mavericks

user1113569
  • 3,441
  • 2
  • 14
  • 10

1 Answers1

0

The error message is telling you what the problem is.

NameError: name 'file' is not defined

In this case you are calling a function named file but haven't defined or imported a function named find.

Did you mean to call open instead? For example:

append_pdf(PdfFileReader(open(fileName, "rb")),output)
                         ^^^^
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685