Skip to content

Instantly share code, notes, and snippets.

@kywthiha
Forked from anonymous/spread3.py
Created November 17, 2019 10:33
Show Gist options
  • Save kywthiha/748ec376d49411477ceba025299297ae to your computer and use it in GitHub Desktop.
Save kywthiha/748ec376d49411477ceba025299297ae to your computer and use it in GitHub Desktop.
import sys
import os
import csv
from PyQt5.QtWidgets import QTableWidget, QApplication, QMainWindow, QTableWidgetItem, QFileDialog
class MyTable(QTableWidget):
def __init__(self, r, c):
super().__init__(r, c)
self.check_change = True
self.init_ui()
def init_ui(self):
self.cellChanged.connect(self.c_current)
self.show()
def c_current(self):
if self.check_change:
row = self.currentRow()
col = self.currentColumn()
value = self.item(row, col)
value = value.text()
print("The current cell is ", row, ", ", col)
print("In this cell we have: ", value)
def open_sheet(self):
self.check_change = False
path = QFileDialog.getOpenFileName(self, 'Open CSV', os.getenv('HOME'), 'CSV(*.csv)')
if path[0] != '':
with open(path[0], newline='') as csv_file:
self.setRowCount(0)
self.setColumnCount(10)
my_file = csv.reader(csv_file, delimiter=',', quotechar='|')
for row_data in my_file:
row = self.rowCount()
self.insertRow(row)
if len(row_data) > 10:
self.setColumnCount(len(row_data))
for column, stuff in enumerate(row_data):
item = QTableWidgetItem(stuff)
self.setItem(row, column, item)
self.check_change = True
class Sheet(QMainWindow):
def __init__(self):
super().__init__()
self.form_widget = MyTable(10, 10)
self.setCentralWidget(self.form_widget)
col_headers = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
self.form_widget.setHorizontalHeaderLabels(col_headers)
self.form_widget.open_sheet()
self.show()
app = QApplication(sys.argv)
sheet = Sheet()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment