57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
|
import os
|
|
import pypyodbc
|
|
|
|
import csv
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('-o', '--output', default='tables')
|
|
parser.add_argument('wgdaten', metavar='WGDATEN')
|
|
args = parser.parse_args()
|
|
|
|
os.makedirs(args.output, exist_ok=True)
|
|
|
|
print(f'Opening {args.wgdaten}...', flush=True)
|
|
|
|
pypyodbc.lowercase = False
|
|
cnx = pypyodbc.connect(f"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={args.wgdaten};")
|
|
cur = cnx.cursor()
|
|
print(f'Opened {args.wgdaten}!', flush=True)
|
|
|
|
try:
|
|
print(f'Fetching tables...', flush=True)
|
|
tbls = cur.tables(tableType='TABLE')
|
|
tbls = tbls.fetchall()
|
|
print(f'Successfully fetched {len(tbls)} tables!', flush=True)
|
|
|
|
for file, _, t_name, t_type, _ in tbls:
|
|
print(f'Exporting {t_name}...', flush=True)
|
|
|
|
cur.execute(f"SELECT TOP 1 * FROM {t_name};")
|
|
desc = [(t[0], t[1]) for t in cur.description]
|
|
cur.fetchall()
|
|
print(desc, flush=True)
|
|
|
|
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:
|
|
values = [csv.convert_value(val, table=t_name, column=col) for col, val in zip(cols, row)]
|
|
f.write((';'.join(values) + '\n').encode('utf-8'))
|
|
|
|
print(f'Exported {t_name} successfully!', flush=True)
|
|
finally:
|
|
cur.close()
|
|
cnx.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|