Skip to content

Commit ed71306

Browse files
Add files via upload
1 parent 4bf707f commit ed71306

14 files changed

+1660
-0
lines changed

desktop_icon.png

373 Bytes
Loading

document_converter.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
from PyPDF2 import PdfReader
2+
from docx import Document
3+
from docx2pdf import convert
4+
import tabula
5+
import pandas as pd
6+
from openpyxl import load_workbook
7+
from reportlab.platypus import SimpleDocTemplate, Table
8+
from reportlab.lib.pagesizes import letter
9+
from pptx import Presentation
10+
from pdf2image import convert_from_path
11+
import comtypes.client
12+
13+
14+
# Button 1 : PDF to Word format
15+
16+
17+
def convert_pdf_to_word(pdf_path, docx_path):
18+
try:
19+
pdf_reader = PdfReader(pdf_path)
20+
21+
document = Document()
22+
for page in range(len(pdf_reader.pages)):
23+
page_content = pdf_reader.pages[page].extract_text()
24+
document.add_paragraph(page_content)
25+
26+
document.save(docx_path)
27+
28+
return True
29+
except Exception as e:
30+
print(f"Error converting PDF to Word: {str(e)}")
31+
return False
32+
33+
34+
# Button 2 : WORD to PDF format
35+
36+
def convert_word_to_pdf(docx_path, pdf_path):
37+
try:
38+
convert(docx_path, pdf_path)
39+
return True
40+
except Exception as e:
41+
print(f"Error converting Word to PDF: {str(e)}")
42+
return False
43+
44+
45+
# Button 3 : EXCEL to PDF format
46+
47+
def convert_excel_to_pdf(excel_path, pdf_path):
48+
try:
49+
# Load the Excel workbook
50+
workbook = load_workbook(excel_path)
51+
52+
# Select the active sheet
53+
sheet = workbook.active
54+
55+
# Get the data from the sheet
56+
data = sheet.values
57+
58+
# Create a list of rows from the sheet data
59+
rows = list(data)
60+
61+
# Create the PDF document
62+
doc = SimpleDocTemplate(pdf_path, pagesize=letter)
63+
64+
# Create the table from the rows
65+
table = Table(rows)
66+
67+
# Set the table style (optional)
68+
table.setStyle([('GRID', (0, 0), (-1, -1), 1, (0.5, 0.5, 0.5))])
69+
70+
# Build the PDF document with the table
71+
elements = [table]
72+
doc.build(elements)
73+
74+
return True
75+
except Exception as e:
76+
print(f"Error converting Excel to PDF: {str(e)}")
77+
return False
78+
79+
80+
# Button 4 : PDF to EXCEL format
81+
82+
83+
def convert_pdf_to_excel(pdf_path, excel_path):
84+
try:
85+
# Read the PDF and extract tables
86+
tables = tabula.read_pdf(pdf_path, pages='all')
87+
88+
# Convert each table to an Excel sheet
89+
with pd.ExcelWriter(excel_path) as writer:
90+
for i, table in enumerate(tables, start=1):
91+
table.to_excel(writer, sheet_name=f"Sheet{i}", index=False)
92+
93+
return True
94+
except Exception as e:
95+
print(f"Error converting PDF to Excel: {str(e)}")
96+
return False
97+
98+
99+
# Button 5 : PDF to PowerPoint format
100+
101+
def convert_pdf_to_ppt(pdf_path, pptx_path):
102+
try:
103+
# Convert PDF pages to images
104+
images = convert_from_path(pdf_path)
105+
106+
# Create a new PowerPoint presentation
107+
presentation = Presentation()
108+
109+
# Add each image as a new slide to the presentation
110+
for image in images:
111+
slide = presentation.slides.add_slide(presentation.slide_layouts[1])
112+
slide.shapes.add_picture(image, 0, 0)
113+
114+
# Save the PowerPoint presentation
115+
presentation.save(pptx_path)
116+
117+
return True
118+
except Exception as e:
119+
print(f"Error converting PDF to PowerPoint: {str(e)}")
120+
return False
121+
122+
123+
# button 6 : Powerpoint to PDF format
124+
125+
def convert_ppt_to_pdf(pptx_path, pdf_path):
126+
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
127+
powerpoint.Visible = 1
128+
129+
presentation = powerpoint.Presentations.Open(pptx_path)
130+
presentation.ExportAsFixedFormat(pdf_path, 2) # 2 represents the PDF format
131+
132+
presentation.Close()
133+
powerpoint.Quit()
134+
135+
return True

file_format_converter.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from fpdf import FPDF
2+
import pandas as pd
3+
import csv
4+
import openpyxl
5+
6+
# Button 1 : Text to PDF Format
7+
8+
def convert_text_to_pdf(input_file, output_file):
9+
pdf = FPDF()
10+
pdf.add_page()
11+
pdf.set_font("Arial", size=12)
12+
with open(input_file, "r") as file:
13+
text = file.read()
14+
pdf.multi_cell(0, 10, txt=text)
15+
pdf.output(output_file)
16+
17+
18+
19+
20+
# Button 2 : CSV to Excel Format
21+
22+
23+
def convert_csv_to_excel(csv_file, excel_file):
24+
try:
25+
df = pd.read_csv(csv_file)
26+
writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
27+
df.to_excel(writer, index=False)
28+
writer.save()
29+
return True
30+
except Exception as e:
31+
print(f"Error converting CSV to Excel: {str(e)}")
32+
return False
33+
34+
35+
36+
# Button 3 : Excel to CSV format
37+
38+
def convert_excel_to_csv(excel_file, csv_file):
39+
try:
40+
wb = openpyxl.load_workbook(excel_file)
41+
sheet = wb.active
42+
43+
with open(csv_file, 'w', newline='') as file:
44+
csv_writer = csv.writer(file)
45+
for row in sheet.iter_rows():
46+
csv_writer.writerow([cell.value for cell in row])
47+
48+
return True
49+
except Exception as e:
50+
print(f"Error converting Excel to CSV: {str(e)}")
51+
return False

github_icon.png

1.51 KB
Loading

gmail_icon.png

781 Bytes
Loading

google_drive.png

1.21 KB
Loading

icons8-arrow-16.png

209 Bytes
Loading

image_converter.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from PIL import Image
2+
from fpdf import FPDF
3+
from pdf2image import convert_from_path
4+
import PyPDF2
5+
6+
poppler_path = r"C:\\Users\\paliw\\Downloads\\poppler-23.06.0\\poppler-23.06.0\\utils"
7+
8+
# Button 1: PNG to JPEG
9+
10+
def convert_png_to_jpeg(png_path, jpeg_path):
11+
try:
12+
# Open the PNG image
13+
png_image = Image.open(png_path)
14+
15+
# Convert the image to JPEG format
16+
png_image.convert("RGB").save(jpeg_path, "JPEG")
17+
18+
return True
19+
20+
except Exception as e:
21+
print("Error converting PNG to JPEG:", str(e))
22+
return False
23+
24+
25+
# Button 2; JPEG to PNG
26+
27+
def convert_jpeg_to_png(jpeg_path, png_path):
28+
try:
29+
image = Image.open(jpeg_path)
30+
image.save(png_path, 'PNG')
31+
return True
32+
except Exception as e:
33+
print(f"Error converting JPEG to PNG: {str(e)}")
34+
return False
35+
36+
37+
# Button 3 : JPEG or PNG to PDF
38+
39+
def convert_image_to_pdf(image_path, pdf_path):
40+
try:
41+
# Create a new PDF object
42+
pdf = FPDF()
43+
44+
# Add a new page to the PDF
45+
pdf.add_page()
46+
47+
# Load the image
48+
image = Image.open(image_path)
49+
50+
# Convert the image to RGB mode (if necessary)
51+
if image.mode != "RGB":
52+
image = image.convert("RGB")
53+
54+
# Get the image size
55+
image_width, image_height = image.size
56+
57+
# Calculate the maximum width and height for the PDF page
58+
max_width = pdf.w - (pdf.l_margin + pdf.r_margin)
59+
max_height = pdf.h - (pdf.t_margin + pdf.b_margin)
60+
61+
# Calculate the scaling factor for the image
62+
scale = min(max_width / image_width, max_height / image_height)
63+
64+
# Calculate the scaled dimensions
65+
scaled_width = image_width * scale
66+
scaled_height = image_height * scale
67+
68+
# Calculate the position to center the image on the page
69+
x = (pdf.w - scaled_width) / 2
70+
y = (pdf.h - scaled_height) / 2
71+
72+
# Add the image to the PDF
73+
pdf.image(image_path, x=x, y=y, w=scaled_width, h=scaled_height)
74+
75+
# Save the PDF to the specified output path
76+
pdf.output(pdf_path, "F")
77+
78+
return True
79+
80+
except Exception as e:
81+
print(f"Error converting image to PDF: {str(e)}")
82+
return False
83+
84+
85+
# Button 4 : PDF to JPEG or PNG format
86+
87+
def convert_pdf_to_image(pdf_path, image_path):
88+
try:
89+
# Convert the PDF to a list of PIL.Image objects
90+
images = convert_from_path(pdf_path)
91+
92+
# Save each image as a separate file
93+
for i, image in enumerate(images):
94+
image.save(f"{image_path}_{i + 1}.png", "PNG")
95+
96+
return True
97+
98+
except Exception as e:
99+
print(f"Error converting PDF to image: {str(e)}")
100+
return False

0 commit comments

Comments
 (0)