I am having some issues with a small program I have made that edits Pdfs using pyPdf. I am attempting to pass the last page of the pdf (self.lastpage) as a default parameter to a class method (pageoutput) When I do this I receive the following error:
Traceback (most recent call last):
File "C:\Census\sf1.py", line 5, in <module>
class PdfGet():
File "C:\Census\sf1.py", line 35, in PdfGet
def pageoutput(self,outfile,start_page=0,end_page=self.lastpage):
NameError: name 'self' is not defined
If i simply specify a number as the end_page it works, yet it fails if I use an attribute. This error is a bet cryptic to me. It doesnt seem to be a problem with pypdf as I can print the lastpage of the pdf with no issues. I would greatly appreciate any insight as to what is going on!
Here is my code (I am using the 3.x compatbile version of pypdf if that matters):
from pyPdf import PdfFileWriter, PdfFileReader
import re
import time
class PdfGet():
def __init__(self):
self.initialize()
def initialize(self):
while True:
raw_args = input('Welcome to PdfGet...\n***Please Enter Arugments (infile,outfile,start_page,end_page) OR type "quit" to exit***\n').strip()
if raw_args.lower() == 'quit':
break
print("Converting Pdf...")
self.args = re.split(r',| ',raw_args)
self.opener(*self.args[0:1])
if len(self.args)== 4:
self.pageoutput(*self.args[1:])
elif len(self.args) == 3:
self.pageoutput(*self.args[1:3])
else:
self.pageoutput(*self.args[1:2])
print("Successfuly Converted!")
nextiter = input('Convert Another PDF? (Type "yes" or "no")').lower()
if nextiter == 'no':
break
def opener(self,infile):
self.output = PdfFileWriter()
self.pdf = PdfFileReader(open(infile, "rb"))
self.pagenum = self.pdf.getNumPages()
self.lastpage = self.pagenum+1
print(self.lastpage)
def pageoutput(self,outfile,start_page=0,end_page=self.lastpage):
for i in range (int(start_page)-1,int(end_page)):
self.output.addPage(self.pdf.getPage(i))
outputStream = open(outfile, "wb")
self.output.write(outputStream)
outputStream.close()
if __name__ == "__main__":
PdfGet()
time.sleep(5)
You should rather pass it a default argument to
Noneand then in the method do the assignment yourself.It is not possible to use self in the method declaration because at this stage self is not yet defined (method signatures are read when the module is loaded, and self is available at runtime when the method is called.)