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

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

问题描述

我正在构建一个具有 Django 后端的网站.我希望能够从 django 可以访问的数据库中提供医疗账单数据.但是,我们收到的所有数据都在 Excel 电子表格中.所以我一直在寻找一种从电子表格中获取数据的方法,然后将其导入到 django 模型中.我知道有一些不同的 django 包可以做到这一点,但我很难理解如何使用这些包.最重要的是,我在这个项目中使用了 python 3.我过去曾在 excel 中使用 win32com 进行自动化操作.我可以编写一个可以从电子表格中获取数据的函数.虽然我想弄清楚的是如何将数据写入 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/ 并考虑这个过程:

Use http://www.python-excel.org/ and consider this process:

  1. 创建一个用户可以上传 xls 文件的视图.
  2. 用 xlrd 打开文件.xlrd.open_workbook(filename)
  3. 提取、创建字典以映射您要在数据库中同步的数据.
  4. 使用模型添加、更新或删除信息.

如果您按照流程进行操作,您可以了解很多加载和提取的工作原理以及它如何满足要求.我建议您先在 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天全站免登陆