https://www.youtube.com/watch?v=1-awqIzgdUA
qtablewidgetExcel_in.py--------------------
from ssl import enum_certificates
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QTableWidgetItem, QHeaderView, QHBoxLayout, QVBoxLayout, QPushButton
from PyQt5.QtCore import Qt
import pandas as pd # pip install pandas
# from PyQt5.QtGui import QGuiApplication
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.window_width, self.window_height = 700, 500
self.resize(self.window_width, self.window_height)
layout = QVBoxLayout()
self.setLayout(layout)
self.table = QTableWidget()
layout.addWidget(self.table)
self.button = QPushButton('&Load Data')
self.button.clicked.connect(lambda _, xl_path=excel_file_path, sheet_name=worksheet_name: self.loadExcelData(xl_path, sheet_name))
layout.addWidget(self.button)
def loadExcelData(self, excel_file_dir, worksheet_name):
df = pd.read_excel(excel_file_dir, worksheet_name)
if df.size == 0:
return
df.fillna('',inplace=True)
self.table.setRowCount(df.shape[0])
self.table.setColumnCount(df.shape[1])
self.table.setHorizontalHeaderLabels(df.columns)
# returns pandas array object
for row in df.iterrows():
values = row[1]
for col_index, value in enumerate(values):
if isinstance(value, (float, int)):
value = '{0:0,.0f}'.format(value)
tableItem = QTableWidgetItem(str(value))
self.table.setItem(row[0], col_index, tableItem)
# self.table.setColumnWidth(2, 300)
if __name__ == '__main__':
# don't auto scale when drag app to a different monitor
# QGuiApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.passThrough)
excel_file_path = r"b:\python\vscode\PyQt5_Excel\직원현황.xlsm"
worksheet_name = "직원현황"
app = QApplication(sys.argv)
app.setStyleSheet('''
QWidget {
font-size: 17px;
}
''')
myApp = MyApp()
myApp.show()
try:
sys.exit(app.exec())
except SystemExit:
print('Closing Window...')
'컴퓨터 > Python' 카테고리의 다른 글
python qtablewidget signal (0) | 2022.05.20 |
---|---|
python qtablewidget excel out (0) | 2022.05.19 |
pyautogui (0) | 2022.05.18 |
python def __init__(self, parent=None)의 의미 (0) | 2022.05.17 |
python qtablewidget excel 실행 (0) | 2022.05.17 |