【问题标题】:PyQt: Import .xls file and populate QTableWidget?PyQt:导入 .xls 文件并填充 QTableWidget?
【发布时间】:2012-08-02 18:25:29
【问题描述】:

如何使用 Python 和 PyQt 导入 .xls 文件,然后使用其内容填充 QTableWidget?

【问题讨论】:

    标签: python pyqt xls qtablewidget


    【解决方案1】:

    我使用xlrd 读写.xls 文件示例herehere

    【讨论】:

    • 我不知道哪个最好,但对于我的项目,xlrd 一直很有希望
    【解决方案2】:

    试试我的图书馆pyexcel

    假设你有一个 csv、xls、xlsx 文件如下:

    1,2,3
    4,5,6
    7,8,9
    

    以下代码将为您提供 json 中的数据

    from pyexcel as pe
    import json
    
    # "example.xls", please import pyexel.ext.xls
    # "example.xlsx", please import pyexcel.ext.xlsx
    # "example.ods", please import pyexcel.ext.ods or pyexcel.ext.ods3
    sheet= pe.load("example.csv")
    print json.dumps(sheet.to_array())
    

    输出是:

    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    

    您可以轻松地初始化QTableWidiget(sheet.number_of_rows(), sheet.number_of_columns()),然后用数组data 填充表格小部件。

    更多代码示例可以在这个页面找到:https://github.com/chfw/pyexcel

    API 文档可以在这里找到:http://pyexcel.readthedocs.org/en/latest/api.html

    【讨论】:

    • 嘿伙计,有点晚了,但还是不错的答案... :)
    【解决方案3】:

    对于导入和读取 .xls 文件,我会从 xlutils 开始,特别是 xlrd

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-03
      • 2022-06-13
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-09-18
      • 2019-12-18
      相关资源
      最近更新 更多