Python processing xls table (i)

Source: Internet
Author: User

This is the introduction of the XLS table content into the data of the embryonic, not perfect, and continue to improve


First step: Define data fields based on Excel tables

models.py

#!/usr/bin/env python#_*_ coding:utf-8 _*_ from django.db import models#  create your models here. #IT资产类型class  it_type (models. Model):     type_name=models. Charfield (max_length=50,)        def __unicode__ (self):         return self. type_name# company type Class company_type (models. Model):     company_name=models. Charfield (MAX_LENGTH=50)         def __unicode__ (self):         return self. company_name         #部门类型class  department_type (models. Model):     department_name=models. Charfield (MAX_LENGTH=50)         def __unicode__ (self):         return self. Department_name     #公司IT资产汇总        class it_assets (models. Model):     #卡片编号     card_number=models. Integerfield (blank=true)      #资产编号     asset_number=models. Integerfield (blank=true)      #采购日期     purchase_date=models. Datefield (blank=true)      #使用年限     useful_life=models. Integerfield (blank=true)      #类别名称     type_it=models. ForeignKey (' It_type ')      #资产名称     name_assets=models. Charfield (max_length=50,blank=true)      #原值     original_value=models. Floatfield (blank=true)      #本月计提值     depreciation_month=models. Floatfield (blank=true)      #本年计提值     year_depreciation=models. Floatfield (blank=true)      #累计折Old     accumulated_depreciation=models. Floatfield (blank=true)      #净值     net_worth=models. Floatfield (blank=true)      #公司类型     type_company=models. ForeignKey (' Company_type ')      #使用人     userful_username=models. Charfield (max_length=50)      #部门类型     type_department=models. ForeignKey (' Department_type ', blank=true)      #存放地     storage_places= Models. Charfield (max_length=30,blank=true)     def __unicode__ (self):         return self. Userful_username


Step Two: Define Views.py

#!/usr/bin/env python#_*_ coding:utf-8 _*_ from django.shortcuts import  renderfrom django.template.context_processors import requestfrom it import  modelsimport xlrdfrom django.http.response import httpresponseimport xlsimport  Datetimeimport time data = xlrd.open_workbook (R ' D:\it_assets.xls ', ' R ') table=data.sheets () [ 0]nrow = table.nrowsprint nrow for i in range (Nrow):     xls = table.row_values (i)      obj = xls[4].split (' \ n ')              #修改IT资产类型def  it_type (Request):     for j in obj :         Models.IT_Type.objects.create (TYPE_NAME=J)         return  HttpResponse (' OK ')  def assets (requEST):     try:        for i in range (1,nrow):                 print  i                      #if  i > 1:                 test = table.row_values (i)                  print i                                   #卡片编号                  v1=test[0]                  #资产编号                  v2=test[1]                  #采购日期                  Date=test[2]                if  date:                     date=date.strip (). Split ('. ')                      date= '-'. Join (date)                                    #使用年限                 v4=test[3]                  #类别名称                  v5=test[4]                  #资产名称                  v6=test[5]                  #原值                  v7=test[6]                  #本月折旧值                 V8 =test[8]                 #本年折旧值                 v9=test[9]                  #累计折旧值                   v10=test[10]                  #净值                  v11=test[12]                  #公司                  v12=test[13]                  #使用人                  v13=test[14]                            #所属部门                      v14=test[15]                                  if v14== ':                     v14= ' None '                  '                  name=models. Department_Type.objects.get (DEPARTMENT_NAME=V14)                      print name                   '                   #存放地                  v15=test[16]                  #Type_Company =models. Company_Type.objects.get (COMPANY_NAME=V12),                  #Type_Department =models. Department_Type.objects.get (DEPARTMENT_NAME=V14),                  #Type_IT =models.it_type.objects.get (TYPE_NAME=V5),                     models.it_ Assets.objects.create (card_number=v1,                                                  Asset_Number=v2,                                                   Purchase_date=date,                                                   Useful_life=v4,                                                  Name_Assets=v6,                                                   Original_Value=v7,                                                   Depreciation_Month=v8,                                                  &nbSp year_depreciation=v9,                                                  accumulated_ depreciation=v10,                                                  net_worth=v11,                                                    Userful_UserName=v13,                                                   Storage_Places=v15,                                                   type_company=models. Company_Type.objects.get (COMPANY_NAME=V12),                                                   type_it=models.it_type.objects.get (TYPE_NAME=V5),                                                   type_department=models. Department_Type.objects.get (DEPARTMENT_NAME=V14),)                           except  exception,e:        print e.message                                       return httpresponse (' Ok ')

Step three: Define the Django Admin Administration page

From Django.contrib import adminfrom IT import models# Register your models here.class it_assetsadmin (admin.                    Modeladmin): List_display = (' Type_company ', ' type_department ', ' name_assets ', ' Userful_username ', ' Type_it ') admin.site.register (models.it_type) admin.site.register (Models.company_type) Admin.site.register (models. Department_type) Admin.site.register (models.it_assets,it_assetsadmin)


Fourth step: Define Urls.py

From Django.conf.urls import include, Urlfrom django.contrib import adminfrom IT import viewsurlpatterns = [url (r ' ^adm In/', include (Admin.site.urls)), url (r ' ^it_type/$ ', views. it_type), url (r ' ^assets/$ ', views. Assets)]

The above operation is only the first time finishing, imperfect and imperfect, and continue to optimize

This article from "Trot Empty" blog, declined reprint!

Python processing xls table (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.