So I wrote this up. It should work but it doesn't. I have labeled my questions (A) and (B) as comments in the script? I feel the logic is correct...yet the script falls apart...
Intent: script is supposed to keep/copy only relevant pages of a pdf. IE PDF file is 20 pages long...I want first and second pages...I type in 0 and 1 and I get PDF2.pdf with only those pages
PyPDF2 is not really my strong suit at all...I reworked code from auto python
Intent: script is supposed to keep/copy only relevant pages of a pdf. IE PDF file is 20 pages long...I want first and second pages...I type in 0 and 1 and I get PDF2.pdf with only those pages
PyPDF2 is not really my strong suit at all...I reworked code from auto python
import PyPDF2, os from functools import partial def my_user_input(question, type, errmsg): while True: asked = input(question) if asked !='': try: asked == type(asked) except ValueError: print(f'"{asked}" is invalid.\n{errmsg}') continue else: print('ok') return asked else: continue os.chdir('/home/me/Downloads/') print(os.getcwd()) str_question = partial(my_user_input,type=str,errmsg='err...something went wrong.') int_question = partial(my_user_input, type=int, errmsg='err...something went wrong\nDon\t forget u must user NUMBERS...') pdf_file_name = str_question('please enter filename') pdf_file_opened = open(pdf_file_name, 'rb') reading = PyPDF2.PdfFileReader(pdf_file_opened) pdfWriter = PyPDF2.PdfFileWriter() #these 2 variables should be integers (A) beginning = int_question('start at what page number? ') finish = int_question('end at what page number? ') #WHY DOES MY SCRIPT DIE HERE? IT SHOULD WORK...NO? while True: #but if I omit int() here I get error suggesting variables are not integers (A) if int(finish) > int(reading.numPages): print(f'there aren\'t {finish} pages in {pdf_file_name}.') finish = int_question('end at what page number? ') else: for num in range(int(beginning), int(finish)): #extract ONLY the pages I want new_page = reading.getPage(num) pdfWriter.addPage(new_page) new_file_name = str_question('please enter new filename WITH .pdf ext...') new_pdf_file = open(new_file_name, 'wb') #write new pages pdfWriter.write(new_pdf_file) new_pdf_file.close()