Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:08:41+00:00 2026-05-24T13:08:41+00:00

I am having some issues with a small program I have made that edits

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:08:42+00:00Added an answer on May 24, 2026 at 1:08 pm

    You should rather pass it a default argument to None and then in the method do the assignment yourself.

    def pageoutput(self, outfile, start_page=0, end_page=None):
        if end_page is None:
            end_page = self.lastpage
    

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
We're having a small issue and could use some help - we have the
I am having some issues with using the OrderBy extension method on a LINQ
I am using Fluent NHibernate and having some issues getting a many to many
I am having some issues with ShareKit for the iPhone. I have everything I
Having some issues with one small function I'm working on for a homework assignment.
We've been having some issues with a SharePoint instance in a test environment. Thankfully
I'm having some issues with producing an int matrix without creating memory leaks. I
We are having some issues getting SharePoint to work as we want it to.
I'm having some inheritance issues as I've got a group of inter-related abstract classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.