PyQt:导入.xls文件并填充QTableWidget?

时间:2012-08-05 14:19:50

标签: python pyqt xls qtablewidget

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

3 个答案:

答案 0 :(得分:1)

试试我的图书馆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

答案 1 :(得分:0)

为了导入和读取.xls文件,我将从xlutils开始,特别是xlrd

答案 2 :(得分:0)

我使用xlrd来阅读和撰写.xls文件示例herehere