wxpython:wx.grid 表格显示 Excel xlsx文件

pip install xlrd

xlrd-1.2.0-py2.py3-none-any.whl (103 kB)

摘要: Library for developers to extract data from Microsoft Excel (tm) spreadsheet files

pip install wxpython==4.2

wxPython-4.2.0-cp37-cp37m-win_amd64.whl (18.0 MB)

Successfully installed wxpython-4.2.0

编写 wx_grid_xlsx.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" wx.grid 表格显示 Excel xlsx文件 """
import os
import sys
import datetime
import xlrd
import wx  
import wx.grid  
  
class MyFrame(wx.Frame):
 
    def __init__(self):  
        super().__init__(parent=None, title='wx.grid view xlsx ', size=(1000,600))
        panel = wx.Panel(self)  

        # 选择.xlsx文件名
        fileFilter = "xlsx Files (*.xlsx)|*.xlsx|" "Excel files (*.xls)|*.xls"
        fileDialog = wx.FileDialog(self, message="选择xlsx文件", wildcard=fileFilter, style=wx.FD_OPEN)
        dialogResult = fileDialog.ShowModal()
        if dialogResult != wx.ID_OK:
            return
        filename = fileDialog.GetPath()
        if not os.path.exists(filename):
            print(f'Error: {filename} not found.')
            sys.exit(2)

        book = xlrd.open_workbook(filename)
        sheetname = "Sheet1"
        sheet = book.sheet_by_name(sheetname)

        nrows = sheet.nrows  # 获取sheet中的有效行数
        if nrows > 10000:
            print(f" 行数: {nrows} > 10000 !")
            nrows = 10000
        ncols = sheet.ncols  # 获取sheet中的有效列数
        if ncols > 26:
            print(f" columns: {ncols} > 26 !")
            ncols = 26
        
        # 创建一个表格  
        self.grid = wx.grid.Grid(panel)  
        self.grid.CreateGrid(nrows, ncols)  # 创建一个 nrows行 ncols列的表格  
  
        # 设置表格的列标题
        cols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        for j in range(0, ncols):
            self.grid.SetColLabelValue(j, cols[j])   
  
        # 设置表格的行标题
        for i in range(0, nrows):
            self.grid.SetRowLabelValue(i, str(i+1))  

        #单元类型cell_type: 0.empty, 1.string, 2.number, 3.date, 4.boolean, 5.error, 6.blank
        # 向表格中添加数据
        for i in range(0, nrows):
            for j in range(0, ncols):
                v = sheet.cell_value(i,j)
                t = sheet.cell_type(i,j)
                if t ==0:
                    self.grid.SetCellValue(i,j, '')
                elif t ==1:
                    self.grid.SetCellValue(i,j, v)
                elif t ==2:
                    if v%1 ==0:
                        self.grid.SetCellValue(i,j, "%d" %v)
                    else:
                        self.grid.SetCellValue(i,j, "%.4f" %v)
                elif t ==3:
                    date_tuple = xlrd.xldate_as_tuple(v, book.datemode)
                    date_v = datetime.date(*date_tuple[:3])
                    self.grid.SetCellValue(i,j, date_v.strftime('%Y-%m-%d'))
                else:
                    self.grid.SetCellValue(i,j, str(v))  
  
        # 使用 sizer 来管理布局  
        sizer = wx.BoxSizer(wx.VERTICAL)  
        sizer.Add(self.grid, 1, wx.EXPAND | wx.ALL, 5)  
        panel.SetSizer(sizer)  


if __name__ == '__main__':  
    app = wx.App(False)  
    frame = MyFrame()
    frame.Show(True)    
    app.MainLoop()

运行 python wx_grid_xlsx.py

参考了百度:文心一言:示例 wx_grid_demo.py

python 复制代码
import wx  
import wx.grid  
  
class MyFrame(wx.Frame):  
    def __init__(self):  
        super().__init__(parent=None, title='Grid Example')  
        panel = wx.Panel(self)  
  
        # 创建一个表格  
        self.grid = wx.grid.Grid(panel)  
        self.grid.CreateGrid(5, 3)  # 创建一个 5 行 3 列的表格  
  
        # 设置表格的列标题  
        self.grid.SetColLabelValue(0, "Column A")  
        self.grid.SetColLabelValue(1, "Column B")  
        self.grid.SetColLabelValue(2, "Column C")  
  
        # 设置表格的行标题  
        self.grid.SetRowLabelValue(0, "Row 1")  
        self.grid.SetRowLabelValue(1, "Row 2")  
        self.grid.SetRowLabelValue(2, "Row 3")  
  
        # 向表格中添加数据  
        self.grid.SetCellValue(0, 0, "Data 1-1")  
        self.grid.SetCellValue(0, 1, "Data 1-2")  
        self.grid.SetCellValue(0, 2, "Data 1-3")  
        self.grid.SetCellValue(1, 0, "Data 2-1")  
        self.grid.SetCellValue(1, 1, "Data 2-2")  
        self.grid.SetCellValue(1, 2, "Data 2-3")  
  
        # 使用 sizer 来管理布局  
        sizer = wx.BoxSizer(wx.VERTICAL)  
        sizer.Add(self.grid, 1, wx.EXPAND | wx.ALL, 5)  
        panel.SetSizer(sizer)  


if __name__ == '__main__':  
    app = wx.App()  
    frame = MyFrame()
    frame.Show()    
    app.MainLoop()
相关推荐
葡萄城技术团队1 天前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
QQ3596773452 天前
ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
数据库·arcgis·excel
星空的资源小屋4 天前
Digital Clock 4,一款免费的个性化桌面数字时钟
stm32·单片机·嵌入式硬件·电脑·excel
揭老师高效办公4 天前
在Excel和WPS表格中批量删除数据区域的批注
excel·wps表格
我是zxb4 天前
EasyExcel:快速读写Excel的工具类
数据库·oracle·excel
辣香牛肉面4 天前
[Windows] 搜索文本2.6.2(从word、wps、excel、pdf和txt文件中查找文本的工具)
word·excel·wps·搜索文本
ljf88384 天前
Java导出复杂excel,自定义excel导出
java·开发语言·excel
tebukaopu1484 天前
json文件转excel
json·excel
shizidushu4 天前
How to work with merged cells in Excel with `openpyxl` in Python?
python·microsoft·excel·openpyxl
Eiceblue5 天前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel