http://blog.163.com/kefan_1987/blog/static/897801312013283264962/ xlrd介绍


1.获取excel文档内容

blue.xlsx内容:

python操作excel_Excel

# encoding : utf-8
import xlrd
xlsfile = r'/soft2/nba/blue.xlsx'
data = xlrd.open_workbook(xlsfile)
table = data.sheet_by_name(u'Sheet2')
nrows = table.nrows
print nrows
for i in range(nrows):
        print table.row_values(i)

打印结果:

[u'Server', u'S_18.21', u'S_18.22', u'S_18.31', u'S_18.32', u'Server', u'S_19.31', u'S_19.32', u'S_19.41', u'S_19.42', u'Server', u'S_19.43', u'S_19.81', u'S_19.82']

[u'/', 0.8, 0.76, 0.8, 0.73, u'/', 0.83, 0.77, 0.89, 0.71, u'/', 0.88, 0.89, 0.6]

[u'/backup', 0.98, 0.26, 0.8, 0.8, u'/backup', 0.7, 0.77, 0.66, 0.82, u'/backup', 0.32, 0.67, 0.33]

……



2.需求很简单:把其它部门整理好的EXCEL文件中的数据,获取出来映射到数据库里去。这里使用了第三包 xlrd 来处理excel。

mms.xlsx内容:

python操作excel_shell_02

#coding:utf-8
import xlrd
ass ={'100.0':20,'101.0':21,'102.0':22,'103.0':23,'104.0':24,'105.0':25,'106.0':26,'107.0':27,'108.0':28,'109.0':29,'110.0':30,'111.0':31,'112.0':32,'113.0':33}
gss ={'101.0':21,'102.0':22,'103.0':23,'104.0':24,'105.0':25,'106.0':26,'107.0':27,'108.0':28,'109.0':29,'110.0':30,'111.0':31,'112.0':32,'113.0':33,'114.0':34}
SQL = 'update 业绩表 set %d = %d,%d = %d where `raw` = %d limit 1;'
wb = xlrd.open_workbook('/soft2/nba/mms.xlsx')
wb.sheet_names()
sh = wb.sheet_by_name(u'Sheet1')

sqlfile = open('/soft1/wb.sql','a')
totalsql = 0

ids = sh.col_values(0)

for k,v in enumerate(ids):
        if isinstance(v,int) or isinstance(v,float):
                v = int(v)
                a = sh.cell(k,2).value
                g = sh.cell(k,3).value
                #a/g转换为str,下面sql中的ass[a]/gss[g]才能正确引用ass/gss中的数据
                a = str(a)
                g = str(g)
                if a!='':

                        #编码转换(中文问题)
                        a = a.encode('gbk')
                        g = g.encode('gbk')
                        #print('%s-->%d-->%s-->%d--%d'%(a,ass[a],g,gss[g],v))
                        sql = SQL%(float(a),ass[a],float(g),gss[g],v)+"\n"
                        totalsql = totalsql + 1
                        print(sql)
                        #写入文件
                        sqlfile.writelines(sql)
                        sqlfile.flush()

sqlfile.close()

print ('程序运行完毕,一共产生了%d条有效SQL。'%(totalsql))

打印结果:

python操作excel_shell_03


以上在读取excel文件时,如果有多个文件,可以考虑使用线程来处理。

先返回所目录下的所有excel文件:

excel_dir = 'D:/tuan_team_back/'
excel_file_array =  []
#返回目录里的excel文件
#os.listdir(excel_dir)
for i in os.listdir(excel_dir):
    f = excel_dir+i
    if os.path.isfile(f):
        t = os.path.splitext(f)[1]
        if t in ['.xls','.xlsx','.xlsm','.xltx','.xlsb']:
            excel_file_array.append(f)
            
print(excel_file_array)