Improved wgexport.py
This commit is contained in:
@ -1,14 +1,14 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
import argparse
|
import argparse
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import decimal
|
|
||||||
import pypyodbc
|
import pypyodbc
|
||||||
|
|
||||||
|
|
||||||
def convert(n, a) -> str:
|
def convert(tbl: str, name: str, a: Any) -> str:
|
||||||
if type(a) == str:
|
if type(a) == str:
|
||||||
return f'"{a}"'
|
return f'"{a}"'
|
||||||
elif type(a) == bool:
|
elif type(a) == bool:
|
||||||
@ -32,18 +32,26 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('wgdaten')
|
parser.add_argument('wgdaten')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
shutil.rmtree(args.output)
|
try:
|
||||||
|
shutil.rmtree(args.output)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
os.makedirs(args.output, exist_ok=True)
|
os.makedirs(args.output, exist_ok=True)
|
||||||
|
|
||||||
|
print(f'Opening {args.wgdaten}...')
|
||||||
pypyodbc.lowercase = False
|
pypyodbc.lowercase = False
|
||||||
conn = pypyodbc.connect(f"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={args.wgdaten};")
|
conn = pypyodbc.connect(f"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};Dbq={args.wgdaten};")
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
print(f'Opened {args.wgdaten}!')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print(f'Fetching tables...')
|
||||||
tbls = cur.tables(tableType='TABLE')
|
tbls = cur.tables(tableType='TABLE')
|
||||||
tbls = tbls.fetchall()
|
tbls = tbls.fetchall()
|
||||||
|
print(f'Successfully fetched {len(tbls)} tables!')
|
||||||
|
|
||||||
for file, _, t_name, t_type, _ in tbls:
|
for file, _, t_name, t_type, _ in tbls:
|
||||||
print(t_name)
|
print(f'Exporting {t_name}...')
|
||||||
|
|
||||||
cur.execute(f"SELECT TOP 1 * FROM {t_name};")
|
cur.execute(f"SELECT TOP 1 * FROM {t_name};")
|
||||||
desc = [(t[0], t[1]) for t in cur.description]
|
desc = [(t[0], t[1]) for t in cur.description]
|
||||||
@ -56,7 +64,9 @@ if __name__ == '__main__':
|
|||||||
with open(f'{args.output}/{t_name}.csv', 'wb+') as f:
|
with open(f'{args.output}/{t_name}.csv', 'wb+') as f:
|
||||||
f.write((';'.join(cols) + '\n').encode('utf-8'))
|
f.write((';'.join(cols) + '\n').encode('utf-8'))
|
||||||
for row in cur:
|
for row in cur:
|
||||||
f.write((';'.join([convert(n, a) for n, a in zip(cols, row)]) + '\n').encode('utf-8'))
|
f.write((';'.join([convert(t_name, n, a) for n, a in zip(cols, row)]) + '\n').encode('utf-8'))
|
||||||
|
|
||||||
|
print(f'Exported {t_name} successfully!')
|
||||||
finally:
|
finally:
|
||||||
cur.close()
|
cur.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
Reference in New Issue
Block a user