qtabwidget qtablewidget显示excel工作表内容(极简excel viewer)

python 复制代码
import sys 
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QLabel,QFileDialog
import getpath
import os
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem
import openpyxl
# Creating the main window 
class MainW(QMainWindow): 
    def __init__(self): 
        super().__init__() 
        self.title = 'PyQt5 - QTabWidget'
        self.left = 0
        self.top = 0
        # self.width = 300
        # self.height = 200
        self.setWindowTitle(self.title) 
        # self.setGeometry(self.left, self.top, self.width, self.height) 

        self.tab_widget = MyTabWidget(self) 
        self.setCentralWidget(self.tab_widget) 
        self.showMaximized() 

# Creating tab widgets 

class MyTabWidget(QWidget): 
    def getBook(self,fname):
        print(fname)
        from openpyxl import load_workbook
        xl_book = load_workbook(filename=fname)
        datas=[]
        titles=[]
        for i in range(len(xl_book.worksheets)):
            table = xl_book.worksheets[i]
            data = []
            titles.append(table.title)
            for row in table.values:
                r = []
                for one in row:
                    if one!=None:
                        r.append(one)
                    else:
                        r.append("")
                data.append(r)
            datas.append(data)
        return (titles,datas)
    def Table(self,data):
        print(data)
        table = QTableWidget()
        table.setRowCount(len(data)) # 设置行数
        if len(data)>0:
            table.setColumnCount(len(data[0]))  # 设置列数
            headers=[openpyxl.utils.get_column_letter(i+1) for i in range(len(data[0]))]
            table.setHorizontalHeaderLabels(headers)
            # 遍历Excel数据并插入到表格控件中
            row = 0
            for row_data in data:
                col = 0
                for cell in row_data:
                    item = QTableWidgetItem(str(cell))
                    table.setItem(row, col, item)
                    col += 1
                row += 1
        return table
    def __init__(self, parent): 
        super(QWidget, self).__init__(parent) 
        self.layout = QVBoxLayout(self) 
        b=QPushButton('open')
        b.clicked.connect(self.open_xlsx)
        self.layout.addWidget(b) 
        # (self.titles,self.datas)=self.getBook(os.path.join(getpath.getpath(),"..","data","spec","low steel.xlsx"))
        self.tab_widget = QTabWidget() 
        # for i in range(len(self.datas)):
        #     one=self.Table(self.datas[i])
        #     self.tab_widget.addTab(one,self.titles[i])#"sheet"+str(i+1))
        self.tab_widget.setTabPosition(QTabWidget.South)
        self.layout.addWidget(self.tab_widget) 
        print(sys.argv)
        if len(sys.argv)>1:
            self.open_file(sys.argv[1])
    def open_file(self,fname):
        print(fname)
        (self.titles,self.datas)=self.getBook(fname)
        self.tab_widget.clear()
        for i in range(len(self.datas)):
            one=self.Table(self.datas[i])
            self.tab_widget.addTab(one,self.titles[i])#"sheet"+str(i+1))
        print(dir(self.tab_widget))
    def open_xlsx(self):
        FileDialog = QFileDialog(self)
        folder=os.path.join(os.path.expanduser('~'), "Desktop")
        fil = "xlsx Files (*.xlsx);;All Files (*)"    
        FileDirectory = FileDialog.getOpenFileName(
            None, 'select import file', folder, fil)  # 选择目录,返回选中的路径
        # logging.info(FileDirectory)
        fname = FileDirectory[0]
        if fname == "":
            pass
        else:
            self.open_file(fname)
if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    ex = App() 
    sys.exit(app.exec_()) 
相关推荐
小学生的信奥之路7 分钟前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
你知道网上冲浪吗1 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
钢铁男儿1 小时前
Python 正则表达式核心元字符全解析
python
武当豆豆1 小时前
C++编程学习(第25天)
开发语言·c++·学习
杨荧2 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
CodeCraft Studio2 小时前
在 Python 中操作 Excel 文件的高效方案 —— Aspose.Cells for Python
python·ui·excel·报表·aspose·aspose.cells
l1t2 小时前
利用DeepSeek辅助WPS电子表格ET格式分析
人工智能·python·wps·插件·duckdb
地平线开发者2 小时前
征程 6 | PTQ 精度调优辅助代码,总有你用得上的
算法·自动驾驶
Tisfy3 小时前
LeetCode 837.新 21 点:动态规划+滑动窗口
数学·算法·leetcode·动态规划·dp·滑动窗口·概率
WSSWWWSSW3 小时前
Matplotlib数据可视化实战:Matplotlib子图布局与管理入门
python·信息可视化·matplotlib