简体   繁体   English

从Excel工作表中获取数据

[英]Getting data from an Excel sheet

How do I load data from an Excel sheet into my Django application? 如何将Excel工作表中的数据加载到我的Django应用程序中? I'm using database PosgreSQL as the database. 我正在使用数据库PosgreSQL作为数据库。

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. 客户希望每周在网站上加载两个不同的列表,他们不希望在管理部分中执行此操作,他们只想从Excel工作表中加载列表。 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. 看看xlrd包,它允许您在Py​​thon中读取Excel文件。 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/ 有关基本用法示例,请查看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. 使用django-batchimport http://code.google.com/p/django-batchimport/它提供了一种将Excel工作表中的数据上传到Django模型的简单方法。 I have used it in a couple of projects. 我在几个项目中使用过它。 It can be integrated very easily into your existing Django project. 它可以很容易地集成到您现有的Django项目中。

Read the documentation on the project page to know how to use it. 阅读项目页面上的文档以了解如何使用它。

It is built on XLRD. 它建立在XLRD上。

Have a look at the presentation "Excel & Python" that Chris Withers gave at PyCon US: 看看Chris Withers在PyCon US上发表的演讲“Excel&Python”:

"This lightning talk explains that you don't need to use COM or be on Windows to read and write native Excel files." “这个闪电般的讲话说明你不需要使用COM或在Windows上读取和写入本机Excel文件。”

http://www.simplistix.co.uk/presentations/python_excel_09/excel-lightning.pdf 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 如果manualy然后只需将excel保存为CSV(使用csv或txt扩展名)并导入到Postgresql中

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. 最好的方法是将此工作表保存为纯文本(CSV或其他)然后加载一些自定义SQL脚本。 http://www.postgresql.org/docs/8.3/static/populate.html 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. 我在xlrd之上构建了django-batchimport,这是令人惊叹的。 The only issues I had were with getting data into Django. 我遇到的唯一问题是将数据导入Django。 Had nothing to do with any limitations of xlrd. 与xlrd的任何限制无关。 It rocks. 它摇滚。 John's work is incredible. 约翰的工作令人难以置信。

Note that I've actually done some update work to django-batchimport and just released. 请注意,我实际上已经对django-batchimport做了一些更新工作,刚刚发布。 Take a look: http://code.google.com/p/django-batchimport/ 看看: http//code.google.com/p/django-batchimport/

Just started using XLRD and it looks very easy and simple to use. 刚开始使用XLRD,它看起来非常简单易用。

Beware that it does not support Excel 2007 yet, so keep in mind to save your excel at 2003 format. 请注意它还不支持Excel 2007,因此请记住以2003格式保存您的Excel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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