-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_convertor.py
More file actions
260 lines (241 loc) · 11.4 KB
/
File_convertor.py
File metadata and controls
260 lines (241 loc) · 11.4 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import os
import pandas as pd
import json
import subprocess
import tempfile
import shutil
from fpdf import FPDF
from pdf2image import convert_from_path
from PIL import Image
from pptx import Presentation
from PyPDF2 import PdfReader
class FileConverter:
@staticmethod
def _validate_extension(file_path, valid_exts, file_desc="file"):
# Ensure the file has one of the valid extensions.
ext = os.path.splitext(file_path)[1].lower()
if ext not in valid_exts:
raise ValueError(f"Invalid extension for {file_desc}: '{ext}'. Expected one of {valid_exts}.")
@staticmethod
def csv_to_excel(csv_file, excel_file):
FileConverter._validate_extension(csv_file, ['.csv'], 'CSV input')
FileConverter._validate_extension(excel_file, ['.xls', '.xlsx'], 'Excel output')
try:
df = pd.read_csv(csv_file)
df.to_excel(excel_file, index=False, engine='openpyxl')
print(f"Converted {csv_file} to {excel_file}.")
except Exception as e:
print(f"Error converting CSV to Excel: {e}")
@staticmethod
def excel_to_csv(excel_file, csv_file):
FileConverter._validate_extension(excel_file, ['.xls', '.xlsx'], 'Excel input')
FileConverter._validate_extension(csv_file, ['.csv'], 'CSV output')
try:
df = pd.read_excel(excel_file, engine='openpyxl')
df.to_csv(csv_file, index=False)
print(f"Converted {excel_file} to {csv_file}.")
except Exception as e:
print(f"Error converting Excel to CSV: {e}")
@staticmethod
def text_to_json(text_file, json_file):
FileConverter._validate_extension(text_file, ['.txt', '.text'], 'Text input')
FileConverter._validate_extension(json_file, ['.json'], 'JSON output')
try:
with open(text_file, 'r', encoding='utf-8') as f:
lines = [line.rstrip('\n') for line in f]
data = {"lines": lines}
with open(json_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
print(f"Converted {text_file} to {json_file}.")
except Exception as e:
print(f"Error converting text to JSON: {e}")
@staticmethod
def json_to_text(json_file, text_file):
FileConverter._validate_extension(json_file, ['.json'], 'JSON input')
FileConverter._validate_extension(text_file, ['.txt'], 'Text output')
try:
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
lines = data.get("lines", [])
with open(text_file, 'w', encoding='utf-8') as f:
f.write("\n".join(lines))
print(f"Converted {json_file} to {text_file}.")
except Exception as e:
print(f"Error converting JSON to text: {e}")
@staticmethod
def text_to_csv(text_file, csv_file):
FileConverter._validate_extension(text_file, ['.txt'], 'Text input')
FileConverter._validate_extension(csv_file, ['.csv'], 'CSV output')
try:
with open(text_file, 'r', encoding='utf-8') as f:
lines = [line.rstrip('\n') for line in f]
df = pd.DataFrame({"lines": lines})
df.to_csv(csv_file, index=False)
print(f"Converted {text_file} to {csv_file}.")
except Exception as e:
print(f"Error converting text to CSV: {e}")
@staticmethod
def html_to_pdf(html_file, pdf_file):
FileConverter._validate_extension(html_file, ['.html', '.htm'], 'HTML input')
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF output')
try:
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
with open(html_file, 'r', encoding='utf-8') as f:
for line in f:
pdf.multi_cell(0, 10, line.strip())
pdf.output(pdf_file)
print(f"Converted {html_file} to {pdf_file}.")
except Exception as e:
print(f"Error converting HTML to PDF: {e}")
@staticmethod
def pdf_to_html(pdf_file, html_file):
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF input')
FileConverter._validate_extension(html_file, ['.html'], 'HTML output')
try:
reader = PdfReader(pdf_file)
with open(html_file, 'w', encoding='utf-8') as f:
f.write("<html><body>")
for page in reader.pages:
text = page.extract_text() or ''
f.write(f"<p>{text.replace('\n', '<br>')}</p>")
f.write("</body></html>")
print(f"Converted {pdf_file} to {html_file}.")
except Exception as e:
print(f"Error converting PDF to HTML: {e}")
@staticmethod
def pdf_to_ppt(pdf_file, ppt_file):
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF input')
FileConverter._validate_extension(ppt_file, ['.pptx'], 'PPTX output')
try:
presentation = Presentation()
for img in convert_from_path(pdf_file):
slide = presentation.slides.add_slide(presentation.slide_layouts[5])
tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
img.save(tmp.name, 'PNG')
slide.shapes.add_picture(tmp.name, 0, 0, width=presentation.slide_width)
tmp.close()
os.unlink(tmp.name)
presentation.save(ppt_file)
print(f"Converted {pdf_file} to {ppt_file}.")
except Exception as e:
print(f"Error converting PDF to PPT: {e}")
@staticmethod
def ppt_to_pdf(ppt_file, pdf_file):
FileConverter._validate_extension(ppt_file, ['.ppt', '.pptx'], 'PPT input')
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF output')
try:
subprocess.run([
'libreoffice', '--headless', '--convert-to', 'pdf', '--outdir',
os.path.dirname(pdf_file) or '.', ppt_file
], check=True)
orig = os.path.join(os.path.dirname(pdf_file),
os.path.splitext(os.path.basename(ppt_file))[0] + '.pdf')
if orig != pdf_file:
shutil.move(orig, pdf_file)
print(f"Converted {ppt_file} to {pdf_file}.")
except Exception as e:
print(f"Error converting PPT to PDF: {e}")
@staticmethod
def pdf_to_jpg(pdf_file, output_folder):
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF input')
try:
os.makedirs(output_folder, exist_ok=True)
for i, img in enumerate(convert_from_path(pdf_file)):
img.save(os.path.join(output_folder, f'page_{i+1}.jpg'), 'JPEG')
print(f"Converted {pdf_file} to JPG images in {output_folder}.")
except Exception as e:
print(f"Error converting PDF to JPG: {e}")
@staticmethod
def jpg_to_pdf(jpg_file, pdf_file):
FileConverter._validate_extension(jpg_file, ['.jpg', '.jpeg'], 'JPG input')
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF output')
try:
img = Image.open(jpg_file)
img.convert('RGB').save(pdf_file)
print(f"Converted {jpg_file} to {pdf_file}.")
except Exception as e:
print(f"Error converting JPG to PDF: {e}")
@staticmethod
def png_to_pdf(png_file, pdf_file):
FileConverter._validate_extension(png_file, ['.png'], 'PNG input')
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF output')
try:
img = Image.open(png_file)
img.convert('RGB').save(pdf_file)
print(f"Converted {png_file} to {pdf_file}.")
except Exception as e:
print(f"Error converting PNG to PDF: {e}")
@staticmethod
def pdf_to_png(pdf_file, output_folder):
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF input')
try:
os.makedirs(output_folder, exist_ok=True)
for i, img in enumerate(convert_from_path(pdf_file)):
img.save(os.path.join(output_folder, f'page_{i+1}.png'), 'PNG')
print(f"Converted {pdf_file} to PNG images in {output_folder}.")
except Exception as e:
print(f"Error converting PDF to PNG: {e}")
@staticmethod
def gif_to_pdf(gif_file, pdf_file):
FileConverter._validate_extension(gif_file, ['.gif'], 'GIF input')
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF output')
try:
img = Image.open(gif_file)
frames = [img.convert('RGB') for _ in range(getattr(img, 'n_frames', 1))]
frames[0].save(pdf_file, save_all=True, append_images=frames[1:])
print(f"Converted {gif_file} to {pdf_file}.")
except Exception as e:
print(f"Error converting GIF to PDF: {e}")
@staticmethod
def pdf_to_gif(pdf_file, gif_file):
FileConverter._validate_extension(pdf_file, ['.pdf'], 'PDF input')
FileConverter._validate_extension(gif_file, ['.gif'], 'GIF output')
try:
frames = [img.convert('RGB') for img in convert_from_path(pdf_file)]
frames[0].save(gif_file, save_all=True, append_images=frames[1:], duration=500, loop=0)
print(f"Converted {pdf_file} to {gif_file}.")
except Exception as e:
print(f"Error converting PDF to GIF: {e}")
@staticmethod
def main():
options = {
'1': ('CSV to Excel', FileConverter.csv_to_excel),
'2': ('Excel to CSV', FileConverter.excel_to_csv),
'3': ('Text to JSON', FileConverter.text_to_json),
'4': ('JSON to Text', FileConverter.json_to_text),
'5': ('Text to CSV', FileConverter.text_to_csv),
'6': ('HTML to PDF', FileConverter.html_to_pdf),
'7': ('PDF to HTML', FileConverter.pdf_to_html),
'8': ('PDF to PPT', FileConverter.pdf_to_ppt),
'9': ('PPT to PDF', FileConverter.ppt_to_pdf),
'10': ('PDF to JPG', FileConverter.pdf_to_jpg),
'11': ('JPG to PDF', FileConverter.jpg_to_pdf),
'12': ('PNG to PDF', FileConverter.png_to_pdf),
'13': ('PDF to PNG', FileConverter.pdf_to_png),
'14': ('GIF to PDF', FileConverter.gif_to_pdf),
'15': ('PDF to GIF', FileConverter.pdf_to_gif)
}
while True:
print("\nOptions:")
for k, (desc, _) in options.items():
print(f"{k}. {desc}")
print("0. Exit")
choice = input("Choice: ").strip().strip('"').strip("'")
if choice == '0':
break
if choice in options:
_, func = options[choice]
src = input("Source path: ").strip().strip('"').strip("'")
if choice in ['10', '13']:
out = input("Output folder: ").strip().strip('"').strip("'")
os.makedirs(out, exist_ok=True)
func(src, out)
else:
dst = input("Destination path: ").strip().strip('"').strip("'")
func(src, dst)
else:
print("Invalid choice.")
if __name__ == '__main__':
FileConverter.main()