-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconverter.py
340 lines (278 loc) · 9.94 KB
/
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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import os #ver12.13.58 docx - xlsx, add try
import logging
from pathlib import Path
from typing import Callable, Dict
import pandas as pd
import numpy as np
from pdf2docx import Converter
from docx import Document
from openpyxl import load_workbook, Workbook
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import markdown
import csv
from PyPDF2 import PdfReader
from pptx import Presentation
from docx2pdf import convert
import mammoth
import html2text
import pdfplumber
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def safe_convert(func: Callable) -> Callable:
"""Decorator to handle exceptions in conversion functions."""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
logging.error(f"Error in {func.__name__}: {str(e)}")
return None
return wrapper
@safe_convert
def convert_pdf_to_docx(pdf_path: str) -> str:
docx_path = Path(pdf_path).with_suffix('.docx')
try:
with Converter(pdf_path) as cv:
cv.convert(str(docx_path))
return str(docx_path)
except Exception as e:
logging.error(f"Error when using pdf2docx: {str(e)}")
logging.info("Trying alternative method...")
try:
pdf = PdfReader(pdf_path)
doc = Document()
for page in pdf.pages:
text = page.extract_text()
doc.add_paragraph(text)
doc.save(docx_path)
return str(docx_path)
except Exception as e:
logging.error(f"Error when using alternative method: {str(e)}")
return None
@safe_convert
def convert_pdf_to_xlsx(pdf_path: str) -> str:
xlsx_path = Path(pdf_path).with_suffix('.xlsx')
pdf = PdfReader(pdf_path)
data = []
for page in pdf.pages:
text = page.extract_text()
data.extend([line.split() for line in text.split('\n') if line.strip()])
df = pd.DataFrame(data)
df.to_excel(xlsx_path, index=False, header=False)
return str(xlsx_path)
@safe_convert
def convert_docx_to_pdf(docx_path: str) -> str:
pdf_path = Path(docx_path).with_suffix('.pdf')
convert(docx_path, str(pdf_path))
return str(pdf_path)
@safe_convert
def convert_xlsx_to_docx(xlsx_path: str) -> str:
docx_path = Path(xlsx_path).with_suffix('.docx')
df = pd.read_excel(xlsx_path)
doc = Document()
for column in df.columns:
doc.add_heading(column, level=1)
for value in df[column]:
doc.add_paragraph(str(value))
doc.add_paragraph() # Add a blank line between columns
doc.save(docx_path)
return str(docx_path)
@safe_convert
def convert_docx_to_xlsx(docx_path: str) -> str:
xlsx_path = Path(docx_path).with_suffix('.xlsx')
doc = Document(docx_path)
data = []
for paragraph in doc.paragraphs:
data.append([paragraph.text])
df = pd.DataFrame(data)
df.to_excel(xlsx_path, index=False, header=False)
return str(xlsx_path)
@safe_convert
def convert_xlsx_to_pdf(xlsx_path: str) -> str:
pdf_path = Path(xlsx_path).with_suffix('.pdf')
df = pd.read_excel(xlsx_path)
pdf = canvas.Canvas(str(pdf_path), pagesize=letter)
y = 750 # Starting y-coordinate
for column in df.columns:
pdf.drawString(100, y, column)
y -= 20
for value in df[column]:
pdf.drawString(120, y, str(value))
y -= 15
if y < 50: # Start a new page if we're near the bottom
pdf.showPage()
y = 750
pdf.save()
return str(pdf_path)
@safe_convert
def convert_xlsx_to_csv(xlsx_path: str) -> str:
csv_path = Path(xlsx_path).with_suffix('.csv')
df = pd.read_excel(xlsx_path)
df.to_csv(csv_path, index=False)
return str(csv_path)
@safe_convert
def convert_txt_to_docx(txt_path: str) -> str:
docx_path = Path(txt_path).with_suffix('.docx')
with open(txt_path, 'r', encoding='utf-8') as file:
text = file.read()
doc = Document()
for paragraph in text.split('\n'):
doc.add_paragraph(paragraph)
doc.save(docx_path)
return str(docx_path)
@safe_convert
def convert_txt_to_pdf(txt_path: str) -> str:
pdf_path = Path(txt_path).with_suffix('.pdf')
with open(txt_path, 'r', encoding='utf-8') as file:
text = file.read()
pdf = canvas.Canvas(str(pdf_path), pagesize=letter)
y = 750 # Starting y-coordinate
for line in text.split('\n'):
pdf.drawString(100, y, line)
y -= 15
if y < 50: # Start a new page if we're near the bottom
pdf.showPage()
y = 750
pdf.save()
return str(pdf_path)
@safe_convert
def convert_txt_to_md(txt_path: str) -> str:
md_path = Path(txt_path).with_suffix('.md')
with open(txt_path, 'r', encoding='utf-8') as file:
txt_content = file.read()
# Simple conversion: assume each line is a paragraph
md_content = '\n\n'.join(txt_content.split('\n'))
with open(md_path, 'w', encoding='utf-8') as file:
file.write(md_content)
return str(md_path)
@safe_convert
def convert_csv_to_xlsx(csv_path: str) -> str:
xlsx_path = Path(csv_path).with_suffix('.xlsx')
df = pd.read_csv(csv_path)
df.to_excel(xlsx_path, index=False)
return str(xlsx_path)
@safe_convert
def convert_pptx_to_pdf(pptx_path: str) -> str:
from win32com import client
pdf_path = Path(pptx_path).with_suffix('.pdf')
powerpoint = client.Dispatch("Powerpoint.Application")
deck = powerpoint.Presentations.Open(pptx_path)
deck.SaveAs(pdf_path, 32) # 32 is the PDF format code
deck.Close()
powerpoint.Quit()
return str(pdf_path)
@safe_convert
def convert_pptx_to_docx(pptx_path: str) -> str:
docx_path = Path(pptx_path).with_suffix('.docx')
presentation = Presentation(pptx_path)
doc = Document()
for slide in presentation.slides:
if slide.shapes.title:
doc.add_heading(slide.shapes.title.text, level=1)
for shape in slide.shapes:
if hasattr(shape, 'text'):
doc.add_paragraph(shape.text)
doc.add_page_break()
doc.save(docx_path)
return str(docx_path)
@safe_convert
def convert_md_to_txt(md_path: str) -> str:
txt_path = Path(md_path).with_suffix('.txt')
with open(md_path, 'r', encoding='utf-8') as file:
md_content = file.read()
h = html2text.HTML2Text()
h.ignore_links = True
txt_content = h.handle(markdown.markdown(md_content))
with open(txt_path, 'w', encoding='utf-8') as file:
file.write(txt_content)
return str(txt_path)
@safe_convert
def convert_md_to_html(md_path: str) -> str:
html_path = Path(md_path).with_suffix('.html')
with open(md_path, 'r', encoding='utf-8') as file:
md_content = file.read()
html_content = markdown.markdown(md_content, extensions=['extra'])
with open(html_path, 'w', encoding='utf-8') as file:
file.write(f"<html><body>{html_content}</body></html>")
return str(html_path)
@safe_convert
def convert_pdf_to_txt(pdf_path: str) -> str:
txt_path = Path(pdf_path).with_suffix('.txt')
pdf = PdfReader(pdf_path)
with open(txt_path, 'w', encoding='utf-8') as file:
for page in pdf.pages:
file.write(page.extract_text())
return str(txt_path)
# Update the CONVERSION_MAP with the new and improved functions
CONVERSION_MAP: Dict[str, Dict[str, Callable]] = {
'.pdf': {
'.docx': convert_pdf_to_docx,
'.xlsx': convert_pdf_to_xlsx,
'.txt': convert_pdf_to_txt,
},
'.xlsx': {
'.docx': convert_xlsx_to_docx,
'.pdf': convert_xlsx_to_pdf,
'.csv': convert_xlsx_to_csv,
},
'.docx': {
'.pdf': convert_docx_to_pdf,
'.xlsx': convert_docx_to_xlsx,
},
'.txt': {
'.docx': convert_txt_to_docx,
'.pdf': convert_txt_to_pdf,
'.md': convert_txt_to_md,
},
'.csv': {
'.xlsx': convert_csv_to_xlsx,
},
'.pptx': {
'.pdf': convert_pptx_to_pdf,
'.docx': convert_pptx_to_docx,
},
'.md': {
'.txt': convert_md_to_txt,
'.html': convert_md_to_html,
},
}
def handle_conversion(file_path: str) -> None:
file_path = Path(file_path)
if not file_path.is_file():
logging.error(f"'{file_path}' is not a valid file or does not exist.")
return
src_ext = file_path.suffix.lower()
if src_ext not in CONVERSION_MAP:
logging.error(f"Source file format not supported: {src_ext}")
return
print(f"Available conversion options for {src_ext}:")
for i, target_ext in enumerate(CONVERSION_MAP[src_ext].keys(), 1):
print(f"{i}. {src_ext[1:].upper()} to {target_ext[1:].upper()}")
choice = input("Enter your choice number: ")
try:
choice = int(choice)
target_ext = list(CONVERSION_MAP[src_ext].keys())[choice - 1]
except (ValueError, IndexError):
logging.error("Invalid choice.")
return
conversion_func = CONVERSION_MAP[src_ext][target_ext]
result = conversion_func(str(file_path))
if result:
logging.info(f"Successfully converted '{file_path}' to '{result}'")
else:
logging.error(f"Conversion failed for '{file_path}'")
def main():
while True:
print("\nFile Format Conversion Program")
print("Please provide the file path using \\ for directory separators.")
file_path = input("Enter the file path (or 'q' to quit): ")
if file_path.lower() == 'q':
print("Thank you for using the program. Goodbye!")
break
handle_conversion(file_path)
choice = input("\nDo you want to convert another file? (y/n): ")
if choice.lower() != 'y':
print("Thank you for using the program. Goodbye!")
break
if __name__ == "__main__":
main()