简体   繁体   中英

Getting data from an Excel sheet

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.

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/

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.

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_excel_09/excel-lightning.pdf

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;

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

或者看看SQLAlchemy,如果你打算写一些脚本来帮助你。( http://www.sqlalchemy.org/

如果要使用COM连接excel(即,您在Windows计算机上运行),请参阅“将Excel数据迁移到SQLite” - http://www.saltycrane.com/blog/2007/11/migrating-excel-to -SQLite-使用-蟒/

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/

Just started using XLRD and it looks very easy and simple to use.

Beware that it does not support Excel 2007 yet, so keep in mind to save your excel at 2003 format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM