views:

447

answers:

8

How do I load data from an Excel sheet into my Django application? I'm using database PosgreSQL as the database.

I want to do this programmatically. A client wants to load two different lists onto the website weekly and they don't want to do it in the admin section, they just want the lists loaded from an Excel sheet. Please help because I'm kind of new here.

A: 

Programatically or manually? If manualy then just save the excel as a CSV (with csv or txt extension) and import into Postgresql using

copy the_data from '/path/to/csv/MYFILE.txt' DELIMITERS ',' CSV;
PurplePilot
programmatically, a client wants to load two different lists onto the website weekly and the don't want to do it in the admin section, they just want the lists loaded from exel sheet. Please help because I'm kind of new here
New to Python/DB/Excel or SO. Please reformat your question with the details in the comment above.
whatnick
A: 

As I remember of this. The best way is to save this sheet as plain text ( CSV or something ) And then load with some custom SQL script. http://www.postgresql.org/docs/8.3/static/populate.html

+5  A: 

Have a look at the xlrd package, which allows you to read Excel files in Python. Once you've read the data you can do whatever you want with it, including saving it to the database.

For a basic usage example, look at http://scienceoss.com/read-excel-files-from-python/

Pär Wieslander
Can `xlrd` read formulas? http://www.lexicon.net/sjmachin/xlrd.htm suggests it can't. https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html suggests it can.
Craig McQueen
I've never tried, so I don't know. But please post your findings if you try it out :-)
Pär Wieslander
Reading through the source for `xlrd` 0.7.1, there is some code in there to parse formulas. But it's disabled by default. And it is experimental/incomplete—e.g. it couldn't handle array formulas that I have in my Excel files.
Craig McQueen
A: 

Or have a look at SQLAlchemy if you're going to write some kind of script to help you with that.(http://www.sqlalchemy.org/)

Pydroid
A: 

If you want to use COM to interface excel (i.e. you are running on a Windows machine), see "Migrating Excel data to SQLite" - http://www.saltycrane.com/blog/2007/11/migrating-excel-to-sqlite-using-python/

wisty
+2  A: 

Have a look at the presentation "Excel & Python" that Chris Withers gave at PyCon US:

"This lightning talk explains that you don't need to use COM or be on Windows to read and write native Excel files."

http://www.simplistix.co.uk/presentations/python%5Fexcel%5F09/excel-lightning.pdf

Drake Moiré
+3  A: 

Use django-batchimport http://code.google.com/p/django-batchimport/ It provides a very simple way to upload data in Excel sheets to your Django models. I have used it in a couple of projects. It can be integrated very easily into your existing Django project.

Read the documentation on the project page to know how to use it.

It is built on XLRD.

Mayuresh
+1 this is a great one to use because they've already coded to some of the quirks in XLRD (which is a great package).
Van Gale
What are the quirks in `xlrd` that need "coding to"?
John Machin
+1  A: 

I built django-batchimport on top of xlrd which is AMAZING. The only issues I had were with getting data into django. Had nothing to do with any limitations of xlrd. It rocks. John's work is incredible.

Note that I've actually done some update work to django-batchimport and just released. Take a look: http://code.google.com/p/django-batchimport/

Keyton