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_()) 
相关推荐
晚风醉蝶1 分钟前
第零篇-算法全景图
算法
GEOshijie12316 分钟前
GEO服务商算法适配承诺怎么验收?48小时响应的合同化考核方案
人工智能·python
周GZ20 分钟前
简单讲解Java中静态方法与 实例方法
java·开发语言
卷无止境29 分钟前
FastAPI 前端托管全攻略:从静态文件到大型全栈项目架构
后端·python
疯狂打码的少年31 分钟前
【数据结构】树的基本概念与二叉树定义
java·数据结构·笔记·算法
webmote3331 分钟前
用 NVIDIA Nemotron 3 Super + .NET 构建有记忆的多轮对话
后端·算法
Keven_1137 分钟前
算法札记:区分稀疏图与稠密图在经验上的数学计算差异
算法·稠密图·稀疏图
QQ5416451211 小时前
【驿帮查件助手python开源】开源聚合查件机器人,实现 24 类驿站批量查件取代短信通知。技术方案分享
python·机器人·开源·驿站查件机器人
卷无止境1 小时前
当FastAPI项目开始"膨胀",代码该往哪儿放
后端·python
用户8356290780511 小时前
Python设置PowerPoint幻灯片背景的方法
后端·python