|
5 | 5 | @author: gattsu997 |
6 | 6 | """ |
7 | 7 |
|
8 | | -#python script to work with pdf docs |
9 | | -#IMPORTANT:: make sure to install PyPDF2 package |
10 | | -#IMPORTANT::keep your pdf file in the SAME file or add path/rename MANUALLY |
| 8 | +# python script to work with pdf docs |
| 9 | +# IMPORTANT:: make sure to install PyPDF2 package |
| 10 | +# IMPORTANT::keep your pdf file in the SAME file or add path/rename MANUALLY |
11 | 11 |
|
12 | 12 |
|
13 | 13 | from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger |
14 | 14 | from pathlib import Path |
15 | 15 |
|
16 | | -print("this script can print, edit, modify, rotate and add passwords to PDF documents if used properly") |
| 16 | +print("this script can print, edit, modify, rotate and add passwords to PDF doc |
| 17 | + uments if used properly") |
17 | 18 |
|
18 | | -pdf_path = (Path.home()/ "myfile.pdf" ) //rename/edit address if different |
| 19 | +pdf_path = (Path.home() / "myfile.pdf") // rename / edit address if different |
19 | 20 | pdf = PdfFileReader(str(pdf_path)) |
20 | 21 |
|
21 | 22 |
|
22 | 23 | print("to get info on your file type INFO") |
23 | | -if input()=="INFO": |
24 | | - print("documeng title", pdf.documentInfo.title,"\n", "from page 0 to",int(pdf.documentInfo.title)-1) |
| 24 | +if input() == "INFO": |
| 25 | + print("documeng title", pdf.documentInfo.title, "\n", "from page 0 to", int |
| 26 | + (pdf.documentInfo.title)-1) |
25 | 27 | print("number of pages:", pdf.getNumPages()) |
26 | | - print("more details:",pdf.documentInfo) |
27 | | - |
28 | | - |
| 28 | + print("more details:", pdf.documentInfo) |
29 | 29 | print("to extract pages type OUTPUT") |
30 | | -if input()=="OUTPUT": |
| 30 | +if input() == "OUTPUT": |
31 | 31 | print("do you want the entire text or a specific page? (FULL/PAGE") |
32 | | - if input()=="FULL": |
| 32 | + if input() == "FULL": |
33 | 33 | for page in pdf.pages: |
34 | 34 | print("text will be saved in a txt file") |
35 | 35 | with open('fulltext.txt', 'w') as f: |
36 | 36 | f.write(page.extractText()) |
37 | | - if input()=="PAGE": |
| 37 | + if input() = ="PAGE": |
38 | 38 | print("which page do you want?") |
39 | | - n=int(input()) |
40 | | - page=pdf.getPage(n) |
| 39 | + n = int(input()) |
| 40 | + page = pdf.getPage(n) |
41 | 41 | print(page.extractText()) |
42 | | - |
43 | | - |
44 | 42 | print("to make a modified pdf type PDF") |
45 | | -if input()=="PDF": |
| 43 | +if input() == "PDF": |
46 | 44 | input_pdf = PdfFileReader(str(pdf_path)) |
47 | | - while input()!="NO": |
| 45 | + while input() != "NO": |
48 | 46 | print("which page do you want to add to new PDF?") |
49 | | - pg=input_pdf.getPage(int(input())) |
| 47 | + pg = input_pdf.getPage(int(input())) |
50 | 48 | pdf_writer = PdfFileWriter() |
51 | 49 | pdf_writer.addPage(pg) |
52 | 50 | with Path("sliced.pdf").open(mode="wb") as output_file: |
53 | 51 | pdf_writer.write(output_file) |
54 | 52 | print("add more pages (YES/NO)") |
55 | | - |
56 | | - |
57 | 53 | print("if you want to concatenate two PDF docs, type ADD") |
58 | | -if input()=="ADD": |
| 54 | +if input() == "ADD": |
59 | 55 | BASE_PATH = (Path.home()) |
60 | 56 | print("name the two files eg file1.pdf, file2.pdf one by one") |
61 | | - m=input() |
62 | | - n=input() |
| 57 | + m = input() |
| 58 | + n = input() |
63 | 59 | pdf_paths = [BASE_PATH / m, BASE_PATH / n] |
64 | 60 | pdf_merger = PdfFileMerger() |
65 | 61 | for path in pdf_paths: |
66 | 62 | pdf_merger.append(str(path)) |
67 | 63 | output_path = Path.home() / "concatenated.pdf" |
68 | 64 | with output_path.open(mode="wb") as output_file: |
69 | 65 | pdf_merger.write(output_file) |
70 | | - |
71 | | - |
72 | | -print("if you want to rotate all pages clockwise write CW or write ACW for anticlockwise") |
73 | | -if input()=="CW": |
| 66 | +print("if you want to rotate all pages clockwise |
| 67 | + write CW or write ACW for anticlockwise") |
| 68 | +if input() == "CW": |
74 | 69 | pdf_reader = PdfFileReader(str(pdf_path)) |
75 | 70 | pdf_writer = PdfFileWriter() |
76 | 71 | for page in pdf_reader.pages: |
77 | 72 | rotated_page = page.rotateClockwise(90) |
78 | 73 | pdf_writer.addPage(rotated_page) |
79 | | -if input=="ACW": |
| 74 | +if input == "ACW": |
80 | 75 | pdf_reader = PdfFileReader(str(pdf_path)) |
81 | 76 | pdf_writer = PdfFileWriter() |
82 | 77 | for page in pdf_reader.pages: |
83 | 78 | rotated_page = page.rotateCounterClockwise(90) |
84 | 79 | pdf_writer.addPage(rotated_page) |
85 | | - |
86 | | - |
87 | 80 | print("if you want to add password to your file type PWD") |
88 | | -if input()=="PWD": |
| 81 | +if input() == "PWD": |
89 | 82 | pdf_reader = PdfFileReader(str(pdf_path)) |
90 | 83 | pdf_writer = PdfFileWriter() |
91 | 84 | pdf_writer.appendPagesFromReader(pdf_reader) |
92 | 85 | print("enter password now") |
93 | | - p=input() |
| 86 | + p = input() |
94 | 87 | pdf_writer.encrypt(user_pwd=p) |
95 | 88 | output_path = Path.home() / "newsletter_protected.pdf" |
96 | 89 | with output_path.open(mode="wb") as output_file: |
97 | 90 | pdf_writer.write(output_file) |
98 | | - |
99 | | - |
100 | | - |
101 | | - |
102 | | - |
103 | | - |
104 | | - |
105 | | - |
106 | | - |
107 | | - |
108 | | - |
109 | | - |
110 | | - |
111 | | - |
112 | | - |
113 | | - |
114 | | - |
115 | | - |
116 | | - |
0 commit comments