|
1 |
| -# 17.2 - Challenge: Use GUI Elements to Help a User Modify Files |
| 1 | +# 17.3 - Challenge: Use GUI Elements to Help a User Modify Files |
2 | 2 | # Solution to challenge
|
3 | 3 |
|
4 | 4 | # save part of a PDF based on a user-supplied page range using a GUI
|
5 | 5 |
|
6 | 6 | importeasyguiasgui
|
7 | 7 | fromPyPDF2importPdfFileReader,PdfFileWriter
|
8 | 8 |
|
9 |
| -#letthe userchoose an inputfile |
| 9 | +#1. Askthe userto select a PDFfile to open. |
10 | 10 | input_file_path=gui.fileopenbox("","Select a PDF to trim...","*.pdf")
|
11 |
| -ifinput_file_pathisNone:# exit on "Cancel" |
12 |
| -exit() |
13 | 11 |
|
14 |
| -#get the page length of the input file |
15 |
| -input_file=PdfFileReader(input_file_path) |
16 |
| -total_pages=input_file.getNumPages() |
| 12 | +#2. If no PDF file is chosen, exit the program. |
| 13 | +ifinput_file_pathisNone: |
| 14 | +exit() |
17 | 15 |
|
18 |
| -#let the user choose a beginning page |
| 16 | +#3. Ask for a starting page number. |
19 | 17 | page_start=gui.enterbox(
|
20 | 18 | "Enter the number of the first page to use:","Where to begin?"
|
21 | 19 | )
|
22 |
| -ifpage_startisNone:# exit on "Cancel" |
| 20 | + |
| 21 | +# 4. If the user does not enter a starting page number, exit the program. |
| 22 | +ifpage_startisNone: |
23 | 23 | exit()
|
24 |
| -# check for possible problems and try again: |
25 |
| -# 1) input page number isn't a (non-negative) digit |
26 |
| -# or 2) input page number is 0 |
27 |
| -# or 3) page number is greater than total number of pages |
| 24 | + |
| 25 | +# 5. Valid page numbers are positive integers. If the user enters an invalid page number: |
| 26 | +# - Warn the user that the entry is invalid |
| 27 | +# - Return to step 3. |
| 28 | +# This solution also checks that the starting page number is not beyond the last page of the PDF!# 3. Asl for a starting page number. |
| 29 | +input_file=PdfFileReader(input_file_path)# Open the PDF |
| 30 | +total_pages=input_file.getNumPages()# Get the total number of pages |
28 | 31 | while (
|
29 | 32 | notpage_start.isdigit()
|
30 | 33 | orpage_start=="0"
|
|
37 | 40 | ifpage_startisNone:# exit on "Cancel"
|
38 | 41 | exit()
|
39 | 42 |
|
40 |
| -#let the user choosean ending page |
| 43 | +#6. Ask foran ending page number |
41 | 44 | page_end=gui.enterbox(
|
42 | 45 | "Enter the number of the last page to use:","Where to end?"
|
43 | 46 | )
|
| 47 | + |
| 48 | +# 7. If the user does not enter and ending page number, exit the program. |
44 | 49 | ifpage_endisNone:# exit on "Cancel"
|
45 | 50 | exit()
|
46 |
| -# check for possible problems and try again: |
47 |
| -# 1) input page number isn't a (non-negative) digit |
48 |
| -# or 2) input page number is 0 |
49 |
| -# or 3) input page number is greater than total number of pages |
50 |
| -# or 4) input page number for end is less than page number for beginning |
| 51 | + |
| 52 | +# 8. If the user enters an invalid page number: |
| 53 | +# - Warn the user that the entry is invalid |
| 54 | +# - Return to step 6. |
| 55 | +# This solution also check that the ending page number is not less than the starting |
| 56 | +# page number, and that it is not greater than the last page in the PDF |
51 | 57 | while (
|
52 | 58 | notpage_end.isdigit()
|
53 | 59 | orpage_end=="0"
|
|
61 | 67 | ifpage_endisNone:# exit on "Cancel"
|
62 | 68 | exit()
|
63 | 69 |
|
64 |
| -#lettheuser choose an output file name |
| 70 | +#9. Ask forthelocation to save the extracted pages |
65 | 71 | output_file_path=gui.filesavebox("","Save the trimmed PDF as...","*.pdf")
|
| 72 | + |
| 73 | +# 10. If the user does not select a save location, exit the program. |
| 74 | +ifoutput_file_pathisNone: |
| 75 | +exit() |
| 76 | + |
| 77 | +# 11. If the chosen save location is the same as the input file path: |
| 78 | +# - Warn the user that they can not overwrite the input file. |
| 79 | +# - Return the step 9. |
66 | 80 | whileinput_file_path==output_file_path:# cannot use same file as input
|
67 | 81 | gui.msgbox(
|
68 | 82 | "Cannot overwrite original file!","Please choose another file..."
|
69 | 83 | )
|
70 | 84 | output_file_path=gui.filesavebox(
|
71 | 85 | "","Save the trimmed PDF as...","*.pdf"
|
72 | 86 | )
|
73 |
| -ifoutput_file_pathisNone: |
74 |
| -exit()# exit on "Cancel" |
| 87 | +ifoutput_file_pathisNone: |
| 88 | +exit() |
75 | 89 |
|
76 |
| -#read in file, write new file with trimmedpagerange and save the new file |
| 90 | +#12. Perform thepageextraction |
77 | 91 | output_PDF=PdfFileWriter()
|
78 |
| -# subtract 1 from supplied start page number to get correct index value |
| 92 | + |
79 | 93 | forpage_numinrange(int(page_start)-1,int(page_end)):
|
80 | 94 | page=input_file.getPage(page_num)
|
81 | 95 | output_PDF.addPage(page)
|
|