63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
|
import datetime
|
|
import os
|
|
import shutil
|
|
import decimal
|
|
import pypyodbc
|
|
|
|
|
|
def convert(n, a) -> str:
|
|
if type(a) == str:
|
|
return f'"{a}"'
|
|
elif type(a) == bool:
|
|
return "T" if a else "F"
|
|
elif a is None:
|
|
return ""
|
|
elif type(a) == datetime.datetime:
|
|
if a.year == 1899 and a.month == 12 and a.day == 30:
|
|
return a.strftime('%H:%M:%S')
|
|
elif a.hour == 0 and a.minute == 0 and a.second == 0:
|
|
return a.strftime('%Y-%m-%d')
|
|
else:
|
|
return str(a)
|
|
else:
|
|
return str(a)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('-o', '--output', default='tables')
|
|
parser.add_argument('wgdaten')
|
|
args = parser.parse_args()
|
|
|
|
shutil.rmtree(args.output)
|
|
os.makedirs(args.output, exist_ok=True)
|
|
|
|
pypyodbc.lowercase = False
|
|
conn = pypyodbc.connect(f"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={args.wgdaten};")
|
|
cur = conn.cursor()
|
|
|
|
try:
|
|
tbls = cur.tables(tableType='TABLE')
|
|
tbls = tbls.fetchall()
|
|
for file, _, t_name, t_type, _ in tbls:
|
|
print(t_name)
|
|
|
|
cur.execute(f"SELECT TOP 1 * FROM {t_name};")
|
|
desc = [(t[0], t[1]) for t in cur.description]
|
|
cur.fetchall()
|
|
print(desc)
|
|
|
|
cur.execute(f"SELECT * FROM {t_name} ORDER BY `{desc[0][0]}`;")
|
|
cols = [t[0] for t in cur.description]
|
|
|
|
with open(f'{args.output}/{t_name}.csv', 'wb+') as f:
|
|
f.write((';'.join(cols) + '\n').encode('utf-8'))
|
|
for row in cur:
|
|
f.write((';'.join([convert(n, a) for n, a in zip(cols, row)]) + '\n').encode('utf-8'))
|
|
finally:
|
|
cur.close()
|
|
conn.close()
|