-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport.py
38 lines (34 loc) · 1.34 KB
/
Export.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
from tkinter import filedialog
import datetime
import csv
import os
import logging
class Export:
def __init__(self, root_window, database, log_handle, close_cb):
self.root_window = root_window
self.database = database
self.close_cb = close_cb
self.log = logging.getLogger('{}.Export'.format(log_handle))
def export_database(self):
now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
options = {}
options['defaultextension'] = '.csv'
options['filetypes'] = [('CSV file', '.csv')]
options['initialdir'] = os.path.expanduser("~")+"/Downloads/"
options['initialfile'] = 'fgr-export-' + now
options['title'] = 'Bewaar database in CSV bestand'
filename = filedialog.asksaveasfilename(**options)
self.log.info('exporteer naar : {}'.format(filename))
try:
f = open(filename, 'w', newline="")
data = self.database.get_registrations_and_guests(True)
writer = csv.writer(f, delimiter=';')
first_item = next(data) # get first item to get keys
writer.writerow(first_item.keys()) # keys=title you're looking for
writer.writerow(first_item)
# write the rest
writer.writerows(data)
f.close()
except:
pass
self.close_cb()