• 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)
    摘要: Cross platform GUI toolkit for Python, "Phoenix" version

    编写 wx_grid_xlsx.py 如下

    1. # -*- coding: utf-8 -*-
    2. """ wx.grid 表格显示 Excel xlsx文件 """
    3. import os
    4. import sys
    5. import datetime
    6. import xlrd
    7. import wx
    8. import wx.grid
    9. class MyFrame(wx.Frame):
    10. def __init__(self):
    11. super().__init__(parent=None, title='wx.grid view xlsx ', size=(1000,600))
    12. panel = wx.Panel(self)
    13. # 选择.xlsx文件名
    14. fileFilter = "xlsx Files (*.xlsx)|*.xlsx|" "Excel files (*.xls)|*.xls"
    15. fileDialog = wx.FileDialog(self, message="选择xlsx文件", wildcard=fileFilter, style=wx.FD_OPEN)
    16. dialogResult = fileDialog.ShowModal()
    17. if dialogResult != wx.ID_OK:
    18. return
    19. filename = fileDialog.GetPath()
    20. if not os.path.exists(filename):
    21. print(f'Error: {filename} not found.')
    22. sys.exit(2)
    23. # Read Excel file
    24. book = xlrd.open_workbook(filename)
    25. sheetname = "Sheet1"
    26. sheet = book.sheet_by_name(sheetname)
    27. nrows = sheet.nrows # 获取sheet中的有效行数
    28. if nrows > 10000:
    29. print(f" 行数: {nrows} > 10000 !")
    30. nrows = 10000
    31. ncols = sheet.ncols # 获取sheet中的有效列数
    32. if ncols > 26:
    33. print(f" columns: {ncols} > 26 !")
    34. ncols = 26
    35. # 创建一个表格
    36. self.grid = wx.grid.Grid(panel)
    37. self.grid.CreateGrid(nrows, ncols) # 创建一个 nrows行 ncols列的表格
    38. # 设置表格的列标题
    39. cols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    40. for j in range(0, ncols):
    41. self.grid.SetColLabelValue(j, cols[j])
    42. # 设置表格的行标题
    43. for i in range(0, nrows):
    44. self.grid.SetRowLabelValue(i, str(i+1))
    45. #单元类型cell_type: 0.empty, 1.string, 2.number, 3.date, 4.boolean, 5.error, 6.blank
    46. # 向表格中添加数据
    47. for i in range(0, nrows):
    48. for j in range(0, ncols):
    49. v = sheet.cell_value(i,j)
    50. t = sheet.cell_type(i,j)
    51. if t ==0:
    52. self.grid.SetCellValue(i,j, '')
    53. elif t ==1:
    54. self.grid.SetCellValue(i,j, v)
    55. elif t ==2:
    56. if v%1 ==0: # int
    57. self.grid.SetCellValue(i,j, "%d" %v)
    58. else: # float
    59. self.grid.SetCellValue(i,j, "%.4f" %v)
    60. elif t ==3: # date
    61. date_tuple = xlrd.xldate_as_tuple(v, book.datemode)
    62. date_v = datetime.date(*date_tuple[:3])
    63. self.grid.SetCellValue(i,j, date_v.strftime('%Y-%m-%d'))
    64. else:
    65. self.grid.SetCellValue(i,j, str(v))
    66. # 使用 sizer 来管理布局
    67. sizer = wx.BoxSizer(wx.VERTICAL)
    68. sizer.Add(self.grid, 1, wx.EXPAND | wx.ALL, 5)
    69. panel.SetSizer(sizer)
    70. if __name__ == '__main__':
    71. app = wx.App(False)
    72. frame = MyFrame()
    73. frame.Show(True)
    74. app.MainLoop()

    运行 python wx_grid_xlsx.py

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

    1. import wx
    2. import wx.grid
    3. class MyFrame(wx.Frame):
    4. def __init__(self):
    5. super().__init__(parent=None, title='Grid Example')
    6. panel = wx.Panel(self)
    7. # 创建一个表格
    8. self.grid = wx.grid.Grid(panel)
    9. self.grid.CreateGrid(5, 3) # 创建一个 5 行 3 列的表格
    10. # 设置表格的列标题
    11. self.grid.SetColLabelValue(0, "Column A")
    12. self.grid.SetColLabelValue(1, "Column B")
    13. self.grid.SetColLabelValue(2, "Column C")
    14. # 设置表格的行标题
    15. self.grid.SetRowLabelValue(0, "Row 1")
    16. self.grid.SetRowLabelValue(1, "Row 2")
    17. self.grid.SetRowLabelValue(2, "Row 3")
    18. # 向表格中添加数据
    19. self.grid.SetCellValue(0, 0, "Data 1-1")
    20. self.grid.SetCellValue(0, 1, "Data 1-2")
    21. self.grid.SetCellValue(0, 2, "Data 1-3")
    22. self.grid.SetCellValue(1, 0, "Data 2-1")
    23. self.grid.SetCellValue(1, 1, "Data 2-2")
    24. self.grid.SetCellValue(1, 2, "Data 2-3")
    25. # 使用 sizer 来管理布局
    26. sizer = wx.BoxSizer(wx.VERTICAL)
    27. sizer.Add(self.grid, 1, wx.EXPAND | wx.ALL, 5)
    28. panel.SetSizer(sizer)
    29. if __name__ == '__main__':
    30. app = wx.App()
    31. frame = MyFrame()
    32. frame.Show()
    33. app.MainLoop()

  • 相关阅读:
    [附源码]SSM计算机毕业设计血库管理系统JAVA
    CentOS7 Hadoop3.3.0 安装与配置
    电缆故障测试仪是什么 都有哪些功能和特点--TFN FB11
    MapStruct的一些常规用法
    GC标记清除算法
    go实现简单gRPC demo
    Leetcode 23.两数相除
    大模型落地实战指南:从选择到训练,深度解析显卡选型、模型训练技、模型选择巧及AI未来展望—打造AI应用新篇章
    day03视图与逻辑
    [oeasy]python0011_ 字符序号_ordinal_ord
  • 原文地址:https://blog.csdn.net/belldeep/article/details/133556080