-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_exporter.py
31 lines (28 loc) · 1.08 KB
/
csv_exporter.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
from PyQt5.QtWidgets import QFileDialog
from PyQt5 import QtCore
from PyQt5 import QtSql
CSV_FILE_FILTER = "CSV files (*.csv)"
class CSV:
@staticmethod
def write_csv(model: QtSql.QSqlQueryModel):
filename, _ = QFileDialog \
.getSaveFileName(None, "Export data to CSV",
".",
CSV_FILE_FILTER, CSV_FILE_FILTER)
data = list()
with open(filename, "a") as f:
# process headers
for i in range(0, model.columnCount()):
header = str(model.headerData(i, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole))
data.append('"' + header + '"')
# write headers
f.write(";".join(data))
f.write("\n")
# process rows
for i in range(model.rowCount()):
data.clear()
for j in range(model.columnCount()):
cell = str(model.data(model.index(i, j)))
data.append('"' + cell + '"')
f.write(";".join(data))
f.write("\n")