将数据从Excel电子表格导入到Django模型 [英] Import data from excel spreadsheet to django model

查看:167
本文介绍了将数据从Excel电子表格导入到Django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个拥有django后端的网站.我希望能够从django可以访问的数据库中提供医疗账单数据.但是,我们收到的所有数据都在excel电子表格中.因此,我一直在寻找一种从电子表格中获取数据,然后将其导入django模型的方法.我知道有一些不同的django软件包可以做到这一点,但是我很难理解如何使用这些软件包.最重要的是,我正在为该项目使用python 3.过去,我曾将win32com用作excel中的自动化工具.我可以编写一个可以从电子表格中获取数据的函数.尽管我想弄清楚的是如何将数据写入Django模型?任何建议表示赞赏.

I'm building a website that'll have a django backend. I want to be able to serve the medical billing data from a database that django will have access to. However, all of the data we receive is in excel spreadsheets. So I've been looking for a way to get the data from a spreadsheet, and then import it into a django model. I know there are some different django packages that can do this, but I'm having a hard time understanding how to use these packages. On top of that I'm using python 3 for this project. I've used win32com for automation stuff in excel in the past. I could write a function that could grab the data from the spreadsheet. Though what I want figure out is how would I write the data to a django model? Any advice is appreciated.

推荐答案

使用 http://www.python- excel.org/并考虑以下过程:

  1. 创建一个用户可以在其中上传xls文件的视图.
  2. 使用xlrd打开文件. xlrd.open_workbook(filename)
  3. 提取,创建字典以映射要在db中同步的数据.
  4. 使用模型来添加,更新或删除信息.
  1. Make a view where user can upload the xls file.
  2. Open the file with xlrd. xlrd.open_workbook(filename)
  3. Extract, create dict to map the data you want to sync in db.
  4. Use the models to add, update or delete the information.

如果遵循该过程,则可以了解很多有关加载和提取如何工作以及如何满足要求的知识.我建议您首先在Shell中执行第2步和第3步,以加快实验速度,并避免使用django视图进行上传/测试/错误.

If you follow the process, you can learn a lot of how loading and extracting works and how does it fits with the requirements. I recommend to you first do the step 2 and 3 in shell to get more quicker experiments and avoid to be uploading/testing/error with a django view.

希望这个启动基础对您有用.

Hope this kickoff base works for you.

这篇关于将数据从Excel电子表格导入到Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆