Antoni4040
Antoni4040

Reputation: 2319

PyQt: Import .xls file and populate QTableWidget?

How can I import an .xls file using Python and PyQt and then populate a QTableWidget with it's contents?

Upvotes: 1

Views: 5165

Answers (3)

chfw
chfw

Reputation: 4592

try my library pyexcel,

Suppose you have a csv, xls, xlsx file as the following:

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

The following code will give you the data in 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())

The output is:

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

You can easily initialize QTableWidiget(sheet.number_of_rows(), sheet.number_of_columns()) and then fill in the table widget with the array, data.

More code examples can be found in this page: https://github.com/chfw/pyexcel

API documentation can be found here: http://pyexcel.readthedocs.org/en/latest/api.html

Upvotes: 1

Rakesh
Rakesh

Reputation: 82785

I use xlrd for reading and writing .xls files examples here and here

Upvotes: 1

DecisionNerd
DecisionNerd

Reputation: 1342

For importing and reading the .xls file, I would start with xlutils, specifically xlrd

Upvotes: 0

Related Questions