This is a follow up question to: PyQt: How to sort QTableView columns(strings and numericals)
Now I am planning to do the same sorting for the excel files
Here i
Below is a demo script which reads both csv and excel files, and should sort dates, ints, floats, and strings correctly.
import sys
import pandas as pd, numpy as np
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.model = QtGui.QStandardItemModel(self)
self.model.setSortRole(QtCore.Qt.UserRole)
self.tableView = QtGui.QTableView()
self.tableView.setSortingEnabled(True)
self.tableView.setModel(self.model)
self.button = QtGui.QPushButton('Open CSV', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.tableView)
layout.addWidget(self.button)
def handleButton(self):
filters = (
'CSV files (*.csv *.txt)',
'Excel Files (*.xls *.xml *.xlsx *.xlsm)',
)
path, filter = QtGui.QFileDialog.getOpenFileNameAndFilter(
self, 'Open File', '', ';;'.join(filters))
if path:
csv = filter.startswith('CSV')
if csv:
dataframe = pd.read_csv(path)
else:
dataframe = pd.read_excel(path)
self.model.setRowCount(0)
dateformat = '%m/%d/%Y'
rows, columns = dataframe.shape
for row in range(rows):
items = []
for column in range(columns):
field = dataframe.iat[row, column]
if csv and isinstance(field, str):
try:
field = pd.to_datetime(field, format=dateformat)
except ValueError:
pass
if isinstance(field, pd.tslib.Timestamp):
text = field.strftime(dateformat)
data = field.timestamp()
else:
text = str(field)
if isinstance(field, np.number):
data = field.item()
else:
data = text
item = QtGui.QStandardItem(text)
item.setData(data, QtCore.Qt.UserRole)
items.append(item)
self.model.appendRow(items)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 150, 600, 400)
window.show()
sys.exit(app.exec_())