|
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 | import easygui as gui
|
7 | 7 | from PyPDF2 import PdfFileReader, PdfFileWriter
|
8 | 8 |
|
9 |
| -# let the user choose an input file |
| 9 | +# 1. Ask the user to select a PDF file to open. |
10 | 10 | input_file_path = gui.fileopenbox("", "Select a PDF to trim...", "*.pdf")
|
11 |
| -if input_file_path is None: # 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 | +if input_file_path is None: |
| 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 |
| -if page_start is None: # exit on "Cancel" |
| 20 | + |
| 21 | +# 4. If the user does not enter a starting page number, exit the program. |
| 22 | +if page_start is None: |
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 | not page_start.isdigit()
|
30 | 33 | or page_start == "0"
|
|
37 | 40 | if page_start is None: # exit on "Cancel"
|
38 | 41 | exit()
|
39 | 42 |
|
40 |
| -# let the user choose an ending page |
| 43 | +# 6. Ask for an 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 | if page_end is None: # 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 | not page_end.isdigit()
|
53 | 59 | or page_end == "0"
|
|
61 | 67 | if page_end is None: # exit on "Cancel"
|
62 | 68 | exit()
|
63 | 69 |
|
64 |
| -# let the user choose an output file name |
| 70 | +# 9. Ask for the location 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 | +if output_file_path is None: |
| 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 | while input_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 |
| -if output_file_path is None: |
74 |
| - exit() # exit on "Cancel" |
| 87 | + if output_file_path is None: |
| 88 | + exit() |
75 | 89 |
|
76 |
| -# read in file, write new file with trimmed page range and save the new file |
| 90 | +# 12. Perform the page extraction |
77 | 91 | output_PDF = PdfFileWriter()
|
78 |
| -# subtract 1 from supplied start page number to get correct index value |
| 92 | + |
79 | 93 | for page_num in range(int(page_start) - 1, int(page_end)):
|
80 | 94 | page = input_file.getPage(page_num)
|
81 | 95 | output_PDF.addPage(page)
|
|
0 commit comments