-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanipulate.py
138 lines (129 loc) · 4.67 KB
/
manipulate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Author: lefkovitj (https://lefkovitzj.github.io)
File Last Modified: 1/6/2025
Project Name: PyPdfApp
File Name: manipulate.py
"""
import fitz
from tkinter import *
from tkinter import filedialog
import os
import sys
def gui_get_file(initial_directory="", limit_filetypes=[]):
""" Open file explorer (using tkinter) to select a file. """
root = Tk() # Create the GUI window.
root.withdraw()
complete_file_path = filedialog.askopenfilename(title="File Select", initialdir = os.getcwd() + "/" + initial_directory, filetypes = limit_filetypes) # Select the file.
root.destroy()
file_path, file_name = os.path.split(complete_file_path) # Get the filepath and filename to return to the user.
return complete_file_path, file_name
class Page_Rotate_PDF():
""" Rotate pages within the document. """
def __init__(self, fitz_doc, save_path):
# Initialize the object.
self.doc = fitz_doc
self.save_path = save_path
def rotate_l(self, page_i):
# Rotate the page (left).
spec_page = self.doc[page_i]
spec_page.set_rotation(spec_page.rotation - 90)
def rotate_r(self, page_i):
# Rotate the page (right).
spec_page = self.doc[page_i]
spec_page.set_rotation(spec_page.rotation + 90)
def get(self):
# Get the updated document object.
return self.doc
def save(self):
# Save the document.
self.doc.save(self.save_path)
class Page_Move_PDF():
""" Re-arrange pages within the document. """
def __init__(self, fitz_doc, save_path):
# Initialize the object.
self.doc = fitz_doc
self.save_path = save_path
def move(self, from_page, to_page):
# Move a page.
if from_page <= len(self.doc) and from_page >= 0:
self.doc.move_page(from_page, to_page)
def get(self):
# Get the updated document object.
return self.doc
def save(self):
# Save the document.
self.doc.save(self.save_path)
class Page_Delete_PDF():
""" Delete pages within the document. """
def __init__(self, fitz_doc, save_path):
# Initialize the object.
self.doc = fitz_doc
self.save_path = save_path
def delete(self, page_i):
# Delete page.
self.doc.delete_page(page_i)
def get(self):
# Get the updated document object.
return self.doc
def save(self):
# Save the document.
self.doc.save(self.save_path)
class Page_Insert_Blank_PDF():
""" Delete pages within the document. """
def __init__(self, fitz_doc, save_path):
# Initialize the object.
self.doc = fitz_doc
self.save_path = save_path
def insert(self, page_i):
# Insert a blank page.
self.doc.insert_page(page_i)
def get(self):
# Get the updated document object.
return self.doc
def save(self):
# Save the document.
self.doc.save(self.save_path)
class Watermark_PDF():
""" Watermark pages within the document. """
def __init__(self, fitz_doc, save_path):
# Initialize the object.
self.doc = fitz_doc
self.save_path = save_path
def watermark(self, page_i, source_image, all_pages=False):
# Insert a the watermark on the selected page(s).
if not all_pages:
page = self.doc[page_i]
if not page.is_wrapped:
page.wrap_contents()
page.insert_image(page.bound(), filename = source_image, overlay = True)
else:
for page in self.doc:
if not page.is_wrapped: # Solution for flipped/rotated watermark without reason. Documentation: https://pymupdf.readthedocs.io/en/latest/recipes-common-issues-and-their-solutions.html#misplaced-item-insertions-on-pdf-pages
page.wrap_contents()
page.insert_image(page.bound(), filename = source_image, overlay = True)
def get(self):
# Get the updated document object.
return self.doc
def save(self):
# Save the document.
self.doc.save(self.save_path)
def create_blank_pdf():
""" Return a PDF item with only one blank page. """
new_doc = fitz.open()
new_doc.new_page()
return new_doc
class Create_Blank_PDF():
""" Delete pages within the document. """
def __init__(self, fitz_doc, save_path):
# Initialize the object.
self.doc = fitz_doc
self.save_path = save_path
def insert(self, page_i):
# Insert a blank page.
self.doc.insert_page(page_i)
def get(self):
# Get the updated document object.
return self.doc
def save(self):
# Save the document.
self.doc.save(self.save_path)