forked from Nagasathvik/Python-Programming-Internship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTASK-8.py
More file actions
61 lines (48 loc) · 1.91 KB
/
Copy pathTASK-8.py
File metadata and controls
61 lines (48 loc) · 1.91 KB
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
import os
from PyPDF2 import PdfMerger, PdfReader, PdfWriter
def merge_pdfs(pdf_list, output_path):
merger = PdfMerger()
for pdf in pdf_list:
if os.path.exists(pdf):
merger.append(pdf)
else:
print(f"File not found: {pdf}")
with open(output_path, 'wb') as output_file:
merger.write(output_file)
print(f'Merged PDF saved as {output_path}')
def split_pdf(input_path, output_folder):
if not os.path.exists(input_path):
print(f"File not found: {input_path}")
return
with open(input_path, 'rb') as input_file:
reader = PdfReader(input_file)
num_pages = len(reader.pages)
for page_num in range(num_pages):
writer = PdfWriter()
writer.add_page(reader.pages[page_num])
output_path = os.path.join(output_folder, f'page_{page_num + 1}.pdf')
with open(output_path, 'wb') as output_file:
writer.write(output_file)
print(f'Saved {output_path}')
def main():
print("PDF Merger/Splitter")
print("1. Merge PDFs")
print("2. Split PDF")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == '1':
pdf_files = input("Enter the PDF files to merge (separated by commas): ").split(',')
pdf_files = [pdf.strip() for pdf in pdf_files] # Strip whitespace
output_path = input("Enter the output path for the merged PDF: ")
merge_pdfs(pdf_files, output_path)
elif choice == '2':
input_path = input("Enter the PDF file to split: ")
output_folder = input("Enter the output folder for the split PDFs: ")
os.makedirs(output_folder, exist_ok=True)
split_pdf(input_path, output_folder)
elif choice == '3':
print("Exiting.")
else:
print("Invalid choice. Exiting.")
if __name__ == "__main__":
main()