-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
85 lines (60 loc) · 2.09 KB
/
index.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
import PyPDF2
import subprocess
import os
from pdf2docx import Converter
#Task1 - Merging two PDF files
def merge_pdf(input_files, output_file):
merger = PyPDF2.PdfMerger()
for file in input_files:
with open(file, 'rb') as f:
merger.append(f)
with open(output_file, 'wb') as f:
merger.write(f)
# Example usage:
input_files1 = ['one.pdf', 'two.pdf']
output_file1 = 'merged.pdf'
merge_pdf(input_files1, output_file1)
#Task2 - Compressing the PDF file
def compress_pdf(input_file, output_file):
# Extract content from PDF
reader = PyPDF2.PdfReader(open(input_file, "rb"))
writer = PyPDF2.PdfWriter()
for page in range(len(reader.pages)):
writer.add_page(reader.pages[page])
# Save extracted content to a temporary file
temp_file = "temp.pdf"
with open(temp_file, "wb") as f:
writer.write(f)
# Compress the temporary file using Ghostscript
subprocess.call(['gswin64', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4',
'-dPDFSETTINGS=/ebook', '-dNOPAUSE', '-dQUIET', '-dBATCH',
'-sOutputFile=' + output_file, temp_file])
# Remove the temporary file
os.remove(temp_file)
# Example usage:
input_file2 = 'one.pdf'
output_file2 = 'compressed.pdf'
compress_pdf(input_file2, output_file2)
# Task3 - Protecting the PDF file
def protect_pdf(input_file, output_file, password):
reader = PyPDF2.PdfReader(open(input_file, 'rb'))
writer = PyPDF2.PdfWriter()
for page_number in range(len(reader.pages)):
writer.add_page(reader.pages[page_number])
writer.encrypt(password)
with open(output_file, 'wb') as f:
writer.write(f)
# Example usage:
input_file3 = 'one.pdf'
output_file3 = 'protected.pdf'
password = 'tariq786'
protect_pdf(input_file3, output_file3, password)
#Task4 - PDF to Word file
def convert_pdf_to_word(input_file, output_file):
cv = Converter(input_file)
cv.convert(output_file, start=0, end=None)
cv.close()
# Example usage:
input_file4 = 'two.pdf'
output_file4 = 'output.docx'
convert_pdf_to_word(input_file4, output_file4)