-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_converter.py
135 lines (95 loc) · 3.67 KB
/
document_converter.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
from PyPDF2 import PdfReader
from docx import Document
from docx2pdf import convert
import tabula
import pandas as pd
from openpyxl import load_workbook
from reportlab.platypus import SimpleDocTemplate, Table
from reportlab.lib.pagesizes import letter
from pptx import Presentation
from pdf2image import convert_from_path
import comtypes.client
# Button 1 : PDF to Word format
def convert_pdf_to_word(pdf_path, docx_path):
try:
pdf_reader = PdfReader(pdf_path)
document = Document()
for page in range(len(pdf_reader.pages)):
page_content = pdf_reader.pages[page].extract_text()
document.add_paragraph(page_content)
document.save(docx_path)
return True
except Exception as e:
print(f"Error converting PDF to Word: {str(e)}")
return False
# Button 2 : WORD to PDF format
def convert_word_to_pdf(docx_path, pdf_path):
try:
convert(docx_path, pdf_path)
return True
except Exception as e:
print(f"Error converting Word to PDF: {str(e)}")
return False
# Button 3 : EXCEL to PDF format
def convert_excel_to_pdf(excel_path, pdf_path):
try:
# Load the Excel workbook
workbook = load_workbook(excel_path)
# Select the active sheet
sheet = workbook.active
# Get the data from the sheet
data = sheet.values
# Create a list of rows from the sheet data
rows = list(data)
# Create the PDF document
doc = SimpleDocTemplate(pdf_path, pagesize=letter)
# Create the table from the rows
table = Table(rows)
# Set the table style (optional)
table.setStyle([('GRID', (0, 0), (-1, -1), 1, (0.5, 0.5, 0.5))])
# Build the PDF document with the table
elements = [table]
doc.build(elements)
return True
except Exception as e:
print(f"Error converting Excel to PDF: {str(e)}")
return False
# Button 4 : PDF to EXCEL format
def convert_pdf_to_excel(pdf_path, excel_path):
try:
# Read the PDF and extract tables
tables = tabula.read_pdf(pdf_path, pages='all')
# Convert each table to an Excel sheet
with pd.ExcelWriter(excel_path) as writer:
for i, table in enumerate(tables, start=1):
table.to_excel(writer, sheet_name=f"Sheet{i}", index=False)
return True
except Exception as e:
print(f"Error converting PDF to Excel: {str(e)}")
return False
# Button 5 : PDF to PowerPoint format
def convert_pdf_to_ppt(pdf_path, pptx_path):
try:
# Convert PDF pages to images
images = convert_from_path(pdf_path)
# Create a new PowerPoint presentation
presentation = Presentation()
# Add each image as a new slide to the presentation
for image in images:
slide = presentation.slides.add_slide(presentation.slide_layouts[1])
slide.shapes.add_picture(image, 0, 0)
# Save the PowerPoint presentation
presentation.save(pptx_path)
return True
except Exception as e:
print(f"Error converting PDF to PowerPoint: {str(e)}")
return False
# button 6 : Powerpoint to PDF format
def convert_ppt_to_pdf(pptx_path, pdf_path):
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
presentation = powerpoint.Presentations.Open(pptx_path)
presentation.ExportAsFixedFormat(pdf_path, 2) # 2 represents the PDF format
presentation.Close()
powerpoint.Quit()
return True