Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyPDF2 script problem
#4
Just a script that extracts full PAGES from a pdf and creates a new pdf file from the extraction.

It uses PyPDF2.

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

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)-1, int(finish)):
        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')
pdfWriter.write(new_pdf_file)
new_pdf_file.close()
Reply


Messages In This Thread
PyPDF2 script problem - by mepyyeti - Mar-13-2018, 12:18 AM
RE: PyPDF2 script problem - by Larz60+ - Mar-13-2018, 02:16 AM
RE: PyPDF2 script problem - by mepyyeti - Mar-13-2018, 03:37 AM
RE: PyPDF2 script problem - by micseydel - Mar-13-2018, 04:19 AM
RE: PyPDF2 script problem - by Larz60+ - Mar-13-2018, 04:27 AM
RE: PyPDF2 script problem - by buran - Mar-13-2018, 11:22 AM
RE: PyPDF2 script problem - by buran - Mar-13-2018, 11:52 AM
EXTRACT pdf PAGES into NEW pdf file - by mepyyeti - Mar-13-2018, 03:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyPDF2 deprecation problem gowb0w 5 4,468 Sep-21-2023, 12:38 PM
Last Post: Pedroski55
  ModuleNotFoundError: No module named 'PyPDF2' Benitta2525 1 1,638 Aug-07-2023, 05:32 AM
Last Post: DPaul
  Pypdf2 will not find text standenman 2 966 Feb-03-2023, 10:52 PM
Last Post: standenman
  pyPDF2 PDFMerger close pensding file japo85 2 2,489 Jul-28-2022, 09:49 AM
Last Post: japo85
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,588 Sep-27-2021, 05:40 PM
Last Post: rondon442
  Problem executing a script on a remote host tester_V 3 2,528 Sep-26-2021, 04:25 AM
Last Post: tester_V
  PyPDF2 processing problem Pavel_47 6 9,848 May-04-2021, 06:58 AM
Last Post: chaitanya
  problem with sphinx and file directory in script kiyoshi7 0 2,315 Mar-11-2021, 03:52 PM
Last Post: kiyoshi7
  problem about slope in python script for bitcoin trading fisher_garry 1 2,538 Sep-02-2020, 01:39 PM
Last Post: fisher_garry
  Problem running script within console koepjo 3 9,955 Mar-26-2020, 07:11 AM
Last Post: koepjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020