|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Wed Oct 27 13:49:16 2021 |
| 4 | +
|
| 5 | +@author: gattsu997 |
| 6 | +""" |
| 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 |
| 11 | + |
| 12 | + |
| 13 | +from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +print("this script can print, edit, modify, rotate and add passwords to PDF documents if used properly") |
| 17 | +pdf_path = (Path.home() / "myfile.pdf") |
| 18 | +pdf = PdfFileReader(str(pdf_path)) |
| 19 | +print("to get info on your file type INFO") |
| 20 | +if input() == "INFO": |
| 21 | + print("documeng title", pdf.documentInfo.title, "\n", "from page 0 to", int |
| 22 | + (pdf.documentInfo.title) - 1) |
| 23 | + print("number of pages:", pdf.getNumPages()) |
| 24 | + print("more details:", pdf.documentInfo) |
| 25 | +print("to extract pages type OUTPUT") |
| 26 | +if input() == "OUTPUT": |
| 27 | + print("do you want the entire text or a specific page? (FULL/PAGE") |
| 28 | + if input() == "FULL": |
| 29 | + for page in pdf.pages: |
| 30 | + print("text will be saved in a txt file") |
| 31 | + with open('fulltext.txt', 'w') as f: |
| 32 | + f.write(page.extractText()) |
| 33 | + if input() == "PAGE": |
| 34 | + print("which page do you want?") |
| 35 | + n = int(input()) |
| 36 | + page = pdf.getPage(n) |
| 37 | + print(page.extractText()) |
| 38 | +print("to make a modified pdf type PDF") |
| 39 | +if input() == "PDF": |
| 40 | + input_pdf = PdfFileReader(str(pdf_path)) |
| 41 | + while input() != "NO": |
| 42 | + print("which page do you want to add to new PDF?") |
| 43 | + pg = input_pdf.getPage(int(input())) |
| 44 | + pdf_writer = PdfFileWriter() |
| 45 | + pdf_writer.addPage(pg) |
| 46 | + with Path("sliced.pdf").open(mode="wb") as output_file: |
| 47 | + pdf_writer.write(output_file) |
| 48 | + print("add more pages (YES/NO)") |
| 49 | +print("if you want to concatenate two PDF docs, type ADD") |
| 50 | +if input() == "ADD": |
| 51 | + BASE_PATH = (Path.home()) |
| 52 | + print("name the two files eg file1.pdf, file2.pdf one by one") |
| 53 | + m = input() |
| 54 | + n = input() |
| 55 | + pdf_paths = [BASE_PATH / m, BASE_PATH / n] |
| 56 | + pdf_merger = PdfFileMerger() |
| 57 | + for path in pdf_paths: |
| 58 | + pdf_merger.append(str(path)) |
| 59 | + output_path = Path.home() / "concatenated.pdf" |
| 60 | + with output_path.open(mode="wb") as output_file: |
| 61 | + pdf_merger.write(output_file) |
| 62 | +print("if you want to rotate all pages clockwise write CW or write ACW for anticlockwise") |
| 63 | +if input() == "CW": |
| 64 | + pdf_reader = PdfFileReader(str(pdf_path)) |
| 65 | + pdf_writer = PdfFileWriter() |
| 66 | + for page in pdf_reader.pages: |
| 67 | + rotated_page = page.rotateClockwise(90) |
| 68 | + pdf_writer.addPage(rotated_page) |
| 69 | +if input == "ACW": |
| 70 | + pdf_reader = PdfFileReader(str(pdf_path)) |
| 71 | + pdf_writer = PdfFileWriter() |
| 72 | + for page in pdf_reader.pages: |
| 73 | + rotated_page = page.rotateCounterClockwise(90) |
| 74 | + pdf_writer.addPage(rotated_page) |
| 75 | +print("if you want to add password to your file type PWD") |
| 76 | +if input() == "PWD": |
| 77 | + pdf_reader = PdfFileReader(str(pdf_path)) |
| 78 | + pdf_writer = PdfFileWriter() |
| 79 | + pdf_writer.appendPagesFromReader(pdf_reader) |
| 80 | + print("enter password now") |
| 81 | + p = input() |
| 82 | + pdf_writer.encrypt(user_pwd=p) |
| 83 | + output_path = Path.home() / "newsletter_protected.pdf" |
| 84 | + with output_path.open(mode="wb") as output_file: |
| 85 | + pdf_writer.write(output_file) |
0 commit comments