Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c73f80c

Browse files
added an new program PDF-TOOLS
A versatile set of Python-based tools for handling PDF files with ease. From merging and splitting to image conversion and encryption, AIO PDF Tools simplifies various PDF-related tasks. Organize your PDF workflow effortlessly, thanks to a user-friendly command-line interface
1 parent ce10fff commit c73f80c

File tree

2 files changed

+380
-0
lines changed

2 files changed

+380
-0
lines changed

‎PDF-TOOLS/python_script.py

Lines changed: 377 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,377 @@
1+
import os
2+
3+
print('033円[32mENTER the number according to the options!!033円[0m')
4+
print('033円[33mthe INPUT directory of the tools is mentioned place your files accordingly033円[0m ')
5+
print('''033円[34mENTER:033円[0m
6+
7+
033円[36m 1 for pdf merger033円[0m 033円[33m[PDF_merger]033円[0m
8+
033円[36m2 for image to PDF033円[0m 033円[33m[input_img2pdf]033円[0m
9+
033円[36m 3 for pdf encryption033円[0m 033円[33m[PDF_ENCRYPT]033円[0m
10+
033円[36m4 for pdf to image033円[0m 033円[33m[input_pdf_to_img]033円[0m
11+
033円[36m 5 for pdf splitter033円[0m 033円[33m[input_split]033円[0m
12+
033円[36m 6 for pdf splitter(specific pages)033円[0m 033円[33m[input_custom_pages]033円[0m''')
13+
print('''when trying for the first time you may notice error.
14+
first add the files according to the directories and then perform the actions''')
15+
16+
17+
def create_directories():
18+
required_directories = ['PDF_Merger', 'Merged_pdf', 'input_img2pdf', 'img2pdf_output', 'PDF_ENCRYPT',
19+
'ENCRYPTED_PDF', 'input_pdf_to_img', 'output_pdf_to_img', 'input_split',
20+
'output_split', 'input_custom_pages', 'output_custom_pages']
21+
for directory in required_directories:
22+
if not os.path.exists(directory):
23+
os.makedirs(directory)
24+
25+
26+
create_directories()
27+
while True:
28+
exit_p = False # Variable to check if "exit" is entered
29+
while True:
30+
w = input(
31+
'033円[36mEnter the value for the corresponding action (press 033円[91m exit033円[0m 033円[36mfor ending the '
32+
'program):033円[0m ')
33+
if w.lower() == 'exit':
34+
exit_p = True
35+
break
36+
if w in ['1', '2', '3', '4', '5', '6']:
37+
break
38+
else:
39+
print('Enter a valid value')
40+
41+
if exit_p: # Check if "exit" was entered
42+
break # Break out of the outer while loop to end the program
43+
if w == "1":
44+
print("Place the pdf file(s) in the 'PDF_merger' directory and check out the output at 'merger_pdf' directory")
45+
46+
47+
def merge_pdfs(input_directory, output_directory, output_filename):
48+
try:
49+
# Create the input directory if it doesn't exist
50+
if not os.path.exists(input_directory):
51+
os.makedirs(input_directory)
52+
53+
# Create the output directory if it doesn't exist
54+
if not os.path.exists(output_directory):
55+
os.makedirs(output_directory)
56+
57+
# Get a list of all PDF files in the input directory
58+
input_pdf_files = [os.path.join(input_directory, file) for file in os.listdir(input_directory) if
59+
file.lower().endswith(".pdf")]
60+
61+
if not input_pdf_files:
62+
print("033円[31mNo PDF files found in the 'PDF_merger' directory.033円[0m")
63+
return
64+
65+
# Merge the PDFs
66+
pdf_merger = PyPDF2.PdfMerger()
67+
for pdf_file in input_pdf_files:
68+
with open(pdf_file, "rb") as file:
69+
pdf_merger.append(file)
70+
71+
# Save the merged PDF to the output directory
72+
output_pdf_file = os.path.join(output_directory, output_filename)
73+
with open(output_pdf_file, "wb") as output_file:
74+
pdf_merger.write(output_file)
75+
76+
print(
77+
f"{len(input_pdf_files)} PDF files have been merged and saved as '{output_filename}' in the 'merger_pdf' directory.")
78+
print("033円[42m Task Completed: PDF MERGED 033円[0m")
79+
80+
81+
except Exception as e:
82+
print(f"033円[91mAn error occurred: {e}033円[0m")
83+
84+
85+
create_directories()
86+
87+
if __name__ == "__main__":
88+
input_directory = "PDF_Merger" # Input directory path
89+
output_directory = "Merged_pdf" # Output directory path
90+
output_filename = "merged.pdf" # Output merged PDF filename
91+
92+
merge_pdfs(input_directory, output_directory, output_filename)
93+
94+
elif w == '2':
95+
print("place the images in 'clutterpng directory and check the output at the 'img2pdf_output'")
96+
import img2pdf
97+
98+
99+
def images_to_pdf(input_images, output_pdf):
100+
try:
101+
with open(output_pdf, "wb") as pdf_file:
102+
pdf_file.write(img2pdf.convert(input_images))
103+
104+
print(f"{len(input_images)} images have been converted to PDF: '{output_pdf}'")
105+
print("033円[42m Task Completed: IMG TO PDF 033円[0m")
106+
107+
108+
except Exception as e:
109+
print(f"033円[91mAn error occurred: {e}033円[0m")
110+
111+
112+
if __name__ == "__main__":
113+
input_directory = "input_img2pdf" # Compulsory input directory path (Change if necessary)
114+
output_directory = "img2pdf_output" # Output directory for the PDF (Change if necessary)
115+
output_pdf_file = os.path.join(output_directory, "img2pdf.pdf")
116+
117+
# create the input directory if it doesn't exist
118+
if not os.path.exists(input_directory):
119+
os.makedirs(input_directory)
120+
# Create the output directory if it doesn't exist
121+
if not os.path.exists(output_directory):
122+
os.makedirs(output_directory)
123+
124+
# Get a list of all image files in the input folder
125+
input_image_files = [os.path.join(input_directory, img) for img in os.listdir(input_directory) if
126+
img.lower().endswith((".jpg", ".jpeg", ".png", ".gif"))]
127+
128+
if not input_image_files:
129+
print("033円[31mNo image files found in the 'Clutterpng' folder.033円[0m")
130+
else:
131+
images_to_pdf(input_image_files, output_pdf_file)
132+
133+
create_directories()
134+
135+
elif w == '3':
136+
print("Place the file in the 'PDF_ENCRYPT' directory and check the output in the 'ENCRYPTED_PDF' directory.")
137+
138+
import os
139+
import PyPDF2
140+
141+
142+
# Code for encrypting the PDF file
143+
def encrypt_pdf(input_pdf, output_pdf, password):
144+
try:
145+
with open(input_pdf, "rb") as pdf_file:
146+
pdf_reader = PyPDF2.PdfReader(pdf_file)
147+
pdf_writer = PyPDF2.PdfWriter()
148+
149+
for page_number in range(len(pdf_reader.pages)):
150+
page = pdf_reader.pages[page_number]
151+
pdf_writer.add_page(page)
152+
153+
pdf_writer.encrypt(password)
154+
155+
with open(output_pdf, "wb") as output_file:
156+
pdf_writer.write(output_file)
157+
158+
print(f"PDF file '{input_pdf}' has been encrypted with a password and saved as '{output_pdf}'")
159+
print("033円[42m Task Completed: PDF Encryption 033円[0m")
160+
161+
162+
except Exception as e:
163+
print(f"033円[91mAn error occurred: {e}033円[0m")
164+
165+
166+
create_directories()
167+
168+
if __name__ == "__main__":
169+
while True:
170+
input_directory = "PDF_ENCRYPT"
171+
output_directory = "ENCRYPTED_PDF"
172+
password = input('033円[94mEnter the password you want to give to the file:033円[0m ')
173+
if password.lower() == "exit":
174+
break
175+
176+
input_pdf_file = input('Enter the name of the file you want to encrypt (or type "exit" for MainMenu): ')
177+
if input_pdf_file.lower() == "exit":
178+
break
179+
180+
if input_pdf_file.lower() == "exit":
181+
break
182+
if not input_pdf_file.endswith(".pdf"):
183+
input_pdf_file += ".pdf"
184+
input_pdf_path = os.path.join(input_directory, input_pdf_file)
185+
output_pdf_file = os.path.join(output_directory, f"{input_pdf_file}")
186+
# calling the function
187+
encrypt_pdf(input_pdf_path, output_pdf_file, password)
188+
189+
190+
elif w == '4':
191+
print("place the files at 'input_pdf_to_img' and check the output at 'output_pdf_to_img' ")
192+
import fitz
193+
194+
195+
def pdf_to_image(input_pdf, output_directory):
196+
try:
197+
# Create the output directory if it doesn't exist
198+
if not os.path.exists(output_directory):
199+
os.makedirs(output_directory)
200+
201+
# Convert PDF to images
202+
pdf_document = fitz.open(input_pdf)
203+
204+
for page_number in range(pdf_document.page_count):
205+
page = pdf_document.load_page(page_number)
206+
image = page.get_pixmap(matrix=fitz.Matrix(2, 2)) # You can adjust the matrix for image size
207+
208+
image_path = f"{output_directory}/page_{page_number + 1}.JPG"
209+
image.save(image_path)
210+
211+
print(
212+
f"Page {page_number + 1} of {os.path.basename(input_pdf)} saved as {os.path.basename(image_path)}")
213+
214+
pdf_document.close()
215+
216+
217+
218+
except Exception as e:
219+
print(f"033円[91mAn error occurred: {e}033円[0m")
220+
221+
222+
if __name__ == "__main__":
223+
input_directory = "input_pdf_to_img" # Input directory path
224+
output_directory = "output_pdf_to_img" # Output directory path
225+
226+
# Create the input directory if it doesn't exist
227+
if not os.path.exists(input_directory):
228+
os.makedirs(input_directory)
229+
230+
# Create the output directory if it doesn't exist
231+
if not os.path.exists(output_directory):
232+
os.makedirs(output_directory)
233+
234+
pdf_files = [file for file in os.listdir(input_directory) if file.lower().endswith(".pdf")]
235+
236+
for input_pdf_file in pdf_files:
237+
pdf_to_image(os.path.join(input_directory, input_pdf_file), output_directory)
238+
239+
create_directories()
240+
elif w == '5':
241+
print("place the pdf at 'input_split' and check the output at 'output_split'")
242+
243+
##pdf splitter###
244+
245+
import os
246+
import PyPDF2
247+
248+
249+
def split_pdf(input_directory, input_pdf, output_directory, output_pdf, start_page, end_page):
250+
try:
251+
# Create the output directory if it doesn't exist
252+
if not os.path.exists(output_directory):
253+
os.makedirs(output_directory)
254+
255+
# Get the full paths of the input and output PDF files
256+
input_pdf_path = os.path.join(input_directory, input_pdf)
257+
output_pdf_path = os.path.join(output_directory, output_pdf)
258+
259+
# Open the input PDF file
260+
with open(input_pdf_path, "rb") as pdf_file:
261+
pdf_reader = PyPDF2.PdfReader(pdf_file)
262+
pdf_writer = PyPDF2.PdfWriter()
263+
264+
# Perform page splitting and add pages to the new PDF
265+
for page_num in range(start_page - 1, min(end_page, len(pdf_reader.pages))):
266+
page = pdf_reader.pages[page_num]
267+
pdf_writer.add_page(page)
268+
269+
# Save the new PDF to the output file
270+
with open(output_pdf_path, "wb") as output_file:
271+
pdf_writer.write(output_file)
272+
273+
print(
274+
f"PDF pages {start_page} to {end_page} from '{input_pdf}' have been split and saved as '{output_pdf}'.")
275+
print("033円[42m Task Completed: PDF SPLIT 033円[0m")
276+
277+
278+
except Exception as e:
279+
print(f"033円[91mAn error occurred: {e}033円[0m")
280+
281+
282+
if __name__ == "__main__":
283+
while True:
284+
input_directory = "input_split"
285+
output_directory = "output_split"
286+
try:
287+
input_pdf = input("Enter the name of the input PDF file : ")
288+
if input_pdf.lower() == "exit":
289+
break
290+
output_pdf = input("Enter the desired name for the output PDF file: ")
291+
if output_pdf.lower() == "exit":
292+
break
293+
start_page = int(input("Enter the starting page number: "))
294+
if start_page == "exit":
295+
break
296+
end_page = int(input("Enter the ending page number: "))
297+
if end_page == "exit":
298+
break
299+
300+
if not input_pdf.endswith(".pdf"):
301+
input_pdf += ".pdf"
302+
if not output_pdf.endswith(".pdf"):
303+
output_pdf += ".pdf"
304+
split_pdf(input_directory, input_pdf, output_directory, output_pdf, start_page, end_page)
305+
except Exception as e:
306+
print(f"033円[91mAn error occurred: {e}033円[0m")
307+
308+
309+
elif w == '6':
310+
print("Place the PDF in 'input_custom_pages' and check the output at 'output_custom_pages'")
311+
print("033円[34m(Use this to get the specific pages of pdf )033円[0m ")
312+
313+
import os
314+
import PyPDF2
315+
316+
317+
def split_pdf(input_directory, input_pdf, output_directory, output_pdf, pages_to_split):
318+
try:
319+
# Create the output directory if it doesn't exist
320+
if not os.path.exists(output_directory):
321+
os.makedirs(output_directory)
322+
if not os.path.exists(input_directory):
323+
os.makedirs(input_directory)
324+
325+
# Get the full paths of the input and output PDF files
326+
input_pdf_path = os.path.join(input_directory, input_pdf)
327+
output_pdf_path = os.path.join(output_directory, output_pdf)
328+
329+
# Convert the list of pages to split into integers
330+
pages_to_split = [int(page) for page in pages_to_split.split()]
331+
332+
# Open the input PDF file
333+
with open(input_pdf_path, "rb") as pdf_file:
334+
pdf_reader = PyPDF2.PdfReader(pdf_file)
335+
pdf_writer = PyPDF2.PdfWriter()
336+
337+
# Perform page splitting and add specified pages to the new PDF
338+
for page_num in pages_to_split:
339+
if 1 <= page_num <= len(pdf_reader.pages):
340+
page = pdf_reader.pages[page_num - 1] # Adjust for 0-based index
341+
pdf_writer.add_page(page)
342+
343+
# Save the new PDF to the output file
344+
with open(output_pdf_path, "wb") as output_file:
345+
pdf_writer.write(output_file)
346+
347+
print(
348+
f"PDF pages {', '.join(map(str, pages_to_split))} from '{input_pdf}' have been split and saved as '{output_pdf}'.")
349+
print("033円[42m Task Completed: PDF SPLIT 033円[0m")
350+
351+
except Exception as e:
352+
print(f"033円[91mAn error occurred: {e}033円[0m")
353+
354+
355+
if __name__ == "__main__":
356+
while True:
357+
input_directory = "input_custom_pages"
358+
output_directory = "output_custom_pages"
359+
try:
360+
input_pdf = input("Enter the name of the input PDF file (or type 'exit' to quit): ")
361+
if input_pdf.lower() == "exit":
362+
break
363+
364+
output_pdf = input("Enter the desired name for the output PDF file: ")
365+
if output_pdf.lower() == "exit":
366+
break
367+
pages_to_split = input("Enter the page numbers to split (e.g.:- 1 9 10 11): ")
368+
if pages_to_split.lower() == "exit":
369+
break
370+
371+
if not input_pdf.endswith(".pdf"):
372+
input_pdf += ".pdf"
373+
if not output_pdf.endswith(".pdf"):
374+
output_pdf += ".pdf"
375+
split_pdf(input_directory, input_pdf, output_directory, output_pdf, pages_to_split)
376+
except Exception as e:
377+
print(f"033円[91mAn error occurred: {e}033円[0m")

‎PDF-TOOLS/run.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
python python_script.py
3+
pause

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /