Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tablib Unbundle Databook into Datasets

Tags:

python

I have successfully imported an Excel file into tablib as a Databook.

imported_data = tablib.Databook().load('xlsx',open('file.xlsx', 'rb').read())

Now that I have imported it, I don't seem to be able to do anything with the Databook. I guess I need to get a Dataset (equivalent to one of the Excel worksheets) but I cannot figure out how to unbundle the Databook (or better yet, extract a specific worksheet as a dataset).

Python 2.7.

Tablib reference: http://docs.python-tablib.org/en/latest/api/#tablib.Databook

imported_data
<databook object>
print imported_data <databook object>
imported_data.size: 1
print imported_data[0]: TypeError 
'Databook' object does not support indexing
data = tablib.Dataset(imported_data)
TypeError: 'Databook' object is not iterable

Once I have a dataset, I can get to work on it. Does anyone know how to do this?

like image 316
Alan Avatar asked Jun 17 '26 11:06

Alan


2 Answers

Somehow I've only just started using tablib. In any case I was stumbling through using databooks and encountered this question. No doubt this is no longer a pressing issue, but for anyone else who also finds themselves here the Databook.sheets method returns a list of Dataset objects:

In [2]: databook = tablib.Databook().load('xlsx', open('file.xlsx', 'rb').read())

In [3]: databook.sheets()
Out[3]: [<sheet1 dataset>, <sheet2 dataset>, <sheet3 dataset>]
like image 129
Electric Head Avatar answered Jun 19 '26 01:06

Electric Head


This was the only way I could get the names and the data to come out correctly.
By declaring it was a databook before hand, and what type of file I was imported I was able to access the titles of the datasets and all the data within each dataset.

imported_data = tablib.Databook()  # declare the databook first
imported_data.xlsx = open(import_filename, 'rb').read()  

for dataset in imported_data.sheets():
    print(dataset.title)  # returns all the sheet title names
    print(dataset)  # returns the data in each sheet
like image 29
Tyler C. Avatar answered Jun 19 '26 02:06

Tyler C.