python处理excele表格

1: 安装xlrd----python2.5选择对应版本 (不支持excel2007以上,另存为xls就可以)

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import MySQLdb
import simplejson
from datetime import date, datetime, time
import sys
import xlrd
import xdrlib

#读取execle表格
def open_execel(fileName):
    try:
        data = xlrd.open_workbook(fileName)
        return data
    except Exception,e:
        print str(e)

#读取execle表格数据到数据库
def addDataToDB(data):
    s =  data.sheet_by_index(5) #读取第6个sheet
    for row in range(1, s.nrows):
        if not s.cell(row,0).value:
            break
        value=xlrd.xldate_as_tuple(s.cell(row,0).value, data.datemode) #日期格式
        date = datetime(*value)
        uv= s.cell(row, 1).value
        mau = s.cell(row, 2).value
        dayNewReg = s.cell(row, 3).value
        avgOret = s.cell(row, 4).value
        avgSret = s.cell(row, 5).value
        mNewAdd = s.cell(row, 6).value
        cmReg = s.cell(row,7).value
        if not uv:
            uv = 0
        if not mau:
            mau = 0
        if not dayNewReg:
            dayNewReg=0
        if not avgOret:
            avgOret = float("%.3f"%0.000)
        if not avgSret:
            avgSret = float("%.3f"%0.000)
        if not mNewAdd:
            mNewAdd= 0
        if not cmReg:
            cmReg = 0

 

你可能感兴趣的:(python)