forked from PeanutTheAdmin/FileConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (63 loc) · 3.21 KB
/
main.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
#!/usr/bin/env python3
from wand.image import Image as wi
import os.path
from os import path
import argparse
def get_arguments(): # gets arguments and files from user
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", dest="file", help="File to be converted, (e.g. sample.pdf, apple.png, baseball.jpg).")
parser.add_argument("-ct" , "--convert_type", dest="convert_type", help="New file type to be converted to, (e.g. png or jpg).")
(options) = parser.parse_args()
if not options.file:
parser.error("[-] Please specify a file, use --help for more info.")
elif not options.convert_type:
parser.error("[-] Please specify a file type to be converted to, use --help for more info.")
return options
def check_file(options): # checks if file, and file type to be converted exsits
try:
print(f"[+] Checking if {options.file} exsits.")
if path.exists(options.file) == True:
print(f"[+] File {options.file} found.")
file_name = options.file[:-4]
try:
extension_list = {"pdf": "pdf", "png": "png", "jpg": "jpeg", "gif": "gif", "tif": "tif", "bmp": "bmp", "eps": "eps"}
file_extension = extension_list[options.convert_type]
return file_name, file_extension
except KeyError:
print(f"[-] File type {options.convert_type} to be converted to was not found. Please check the extension before trying again, (e.g. png or jpg).")
else:
print(f"[-] File {options.file} not found. Please check the name and extension before trying again. (e.g. sample.pdf).")
exit()
except Exception as msg:
report_issue(msg)
def convert_file(options, file_name, file_extension): # converts files to other formats
try:
print(f"[+] Converting {options.file} to {file_name}.{options.convert_type}")
pdf = wi(filename=options.file, resolution=300)
pdfimage = pdf.convert(file_extension)
i=1
for img in pdfimage.sequence:
page = wi(image=img)
page.save(filename=f"{file_name}{str(i)}.{options.convert_type}")
i +=1
print("[+] File was successfully converted.")
except Exception as msg:
exception_ghostscript = 'FailedToExecuteCommand `"gswin64c.exe"'
exception_ghostscript_compare = str(msg)
if exception_ghostscript == exception_ghostscript_compare[:38]:
print("[-] File was not successfully converted.\n")
print("There is an issue with ghostscript. Reinstall or download latest version and try again.")
print("Visit: https://github.com/PeanutTheAdmin/FileConverter for install instructions.\n")
else:
print("[-] File was not successfully converted.")
report_issue(msg)
def report_issue(msg):
print(f"[BUG] {msg}\n")
print("To report this issue go to: https://github.com/PeanutTheAdmin/FileConverter/issues")
print("When reporting this issue include the output after [BUG]\n")
def main(): # Main Function
options = get_arguments()
file_name, file_extension = check_file(options)
convert_file(options, file_name, file_extension)
if __name__ == "__main__":
main()