-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (73 loc) · 3.36 KB
/
main.py
File metadata and controls
87 lines (73 loc) · 3.36 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
from __future__ import print_function
import os
import json
from utils.detector import is_pdf, is_ppt, is_docx
from utils.convertor import convert_ppt_to_pdf, convert_docx_to_pdf
class CheckMail:
def __init__(self, root_dir=os.getcwd()):
self.root_dir = root_dir
def convert_to_pdf(self, file_path, folder_path):
"""This will convert the ppt and docx to pdf"""
file = file_path.split('/')[-1]
if is_ppt(file_path):
pdf_file = f'{folder_path}/{file.split(".")[0]}.pdf'
convert_ppt_to_pdf(file_path, pdf_file)
if is_docx(file_path):
pdf_file = f'{folder_path}/{file.split(".")[0]}.pdf'
convert_docx_to_pdf(file_path, pdf_file)
def check_pdf(self, file_path, file):
"""This will check if the file is pdf or not and save in a file with path name. If not then it will remove it."""
if is_pdf(file_path):
print(f'{file} is a PDF.')
if os.path.exists(os.path.join(self.root_dir, 'Pdf_file_paths.json')):
with open(os.path.join(self.root_dir, 'Pdf_file_paths.json'), 'r') as f:
try:
file = json.load(f)
except Exception as e:
print(f"Error: {e}.")
with open(os.path.join(self.root_dir, 'Pdf_file_paths.json'), 'w') as f:
pdf_file_path_array = file['paths']
pdf_file_path_array.append(file_path) if file_path not in pdf_file_path_array else None
json.dump({'paths': pdf_file_path_array}, f)
else:
with open(os.path.join(self.root_dir, 'Pdf_file_paths.json'), 'w') as f:
json.dump({'paths': [file_path]}, f)
else:
try:
os.remove(file_path)
print(f'{file} is not a PDF. So Removed it.')
except OSError as e:
print(f"Error: {e.file} - {e.strerror}.")
def remove_empty_dir(self, dir):
"""This will check if the directory is empty or not. If empty then it will remove it."""
try:
if(os.listdir(dir) == []):
os.rmdir(dir)
except Exception as e:
print(f"Error: {e}.")
def navigate_folder(self, dir):
"""This will navigate through the folder and check if the file is pdf or not. If not then it will convert it to pdf. Else it will remove it."""
for folders in os.listdir(dir):
folder_path = os.path.join(dir, folders)
print('\n')
print(f'Checking folder: {folder_path}')
try:
for file in os.listdir(folder_path):
file_path = os.path.join(folder_path, file)
self.convert_to_pdf(file_path, folder_path)
self.check_pdf(file_path, file)
except NotADirectoryError as e:
print(f"Error: {e}.")
self.remove_empty_dir(folder_path)
def check_dir(self, dir):
"""This will check if the directory is present or not. If not then it will create it."""
try:
os.listdir(dir)
except FileNotFoundError as e:
os.mkdir(dir)
def run(self, dir='Attachments'):
self.check_dir(dir)
self.navigate_folder(dir)
if __name__ == '__main__':
check_mail = CheckMail()
check_mail.run()