-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_file_to_CSV.py
executable file
·97 lines (63 loc) · 2.35 KB
/
access_file_to_CSV.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
import sys
import subprocess
import os
import logging
# Set source directory
os.chdir(os.path.split(sys.argv[1])[0])
'''
configure logging
'''
logging.basicConfig(filename='script.log',filemode='w',format='%(asctime)s - %(levelname)s - %(message)s')
# Directory to output converted csv files
def OutputFileDir(file):
database_path,filename = os.path.split(file)
source_dir = os.path.dirname(database_path)
output_home = os.path.dirname(source_dir)
output_dir = output_home+"/output/"
if not os.path.exists(output_dir):
try:
os.mkdir(output_dir)
except Exception as e:
logging.error("Exception occurred", exc_info=True)
return filename,output_dir
# Handles both .mdb and .accdb
def access_mdb_and_accdb_ToCsv(access_Database):
access_Database_extension = access_Database.rsplit('.',1)[-1]
if(access_Database_extension in ["mdb","MDB","accdb","ACCDB"]):
'''
Get list of tables from access db with "mdb-tables" i.e "mdb-tables database_name".
Works for both .mdb(2003 access version) and .accdb(2007-2013 version - latest access version).
Returns bytes
'''
try:
table_names = subprocess.Popen(["mdb-tables","-1",access_Database],
stdout=subprocess.PIPE).communicate()[0]
except Exception as e:
logging.error("Exception occurred", exc_info=True)
# List of tables
tables = table_names.split()
'''
Dump each returned table as a csv file using "mdb-export" i.e "mdb-export database_name table".
Converting " " in table names to "_" for the csv filenames
'''
for table in tables:
if table != '':
try:
csv_filename = output_dir+table.decode("UTF-8").replace(" ","_")+".csv"
with open(csv_filename,'w') as csv_file:
data = subprocess.Popen(["mdb-export",access_Database,table],
stdout=subprocess.PIPE).communicate()[0]
#write to csv file.
csv_file.write(data.decode('UTF-8'))
# Close the csv file.
csv_file.close()
print("extracted to "+output_dir)
except Exception as e:
logging.error("Exception occurred", exc_info=True)
os.remove(access_Database)
else:
logging.warning('No access file found')
if __name__ == '__main__':
# Output filedir
filename,output_dir = OutputFileDir(sys.argv[1])
access_mdb_and_accdb_ToCsv(filename)