使用xlrd读取excel

一、什么是xlrd模块?

        python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。

二、使用介绍

  1. 引入模块:import xlrd
  2. xlrd模块方法使用:如下例子“解析excel,将单元格内容按照固定格式拼装输出”,简单介绍xlrd模块使用

三、解析excel,将单元格内容按照固定格式拼装输出

# -*- coding: utf-8 -*-
"""
Created on Tue Jul  9 20:42:47 2019

@author: hx
解析excel,将单元格内容按照固定格式拼装输出
"""

import xlrd

def resolveExcel():
    # 获取excel文件
    data = xlrd.open_workbook("C:/Users/Administrator/Desktop/test.xls",encoding_override='utf-8')
    #获取一个excel有多少个sheet
    sheetNames = list(data.sheet_names())
    print(sheetNames)

   #遍历sheet
    for name in sheetNames:
        # 获取sheet
        sheet = data.sheet_by_name(name)
        #打印拼装的resultMap
        packing_part1_resultMap(sheet)
        #打印拼装的where条件
        packing_part2_where_criteria(sheet)
        #打印拼装的update条件
        packing_part3_update_criteria(sheet)
        #打印拼装的insert条件
        packing_part4_insert_criteria(sheet)
              
        # 获取一行的数值
        #table.row_values(i)
        # 获取一列的数值
        #key = sheet.col_values(0)
        #chinese = sheet.col_values(1)
        #获取具体单元格的值
        # cell_value = table.cell(0,1).value
        # print(cell_value)

        #chinestfile = open('/aim/file/location/?.txt', 'a+', encoding='utf-8')
        #chinestfile.write(chineseStr)
        

def packing_part1_resultMap(sheet):
     # 获取总行数
     nrows = sheet.nrows
     chineseStr = ""
     #循环行数
     for row in range(nrows):
         #第一行不计入
         if row !=0:
             #取第一列值
             cell_value1=sheet.cell(row,0).value
             #取第二列值
             cell_value2=sheet.cell(row,1).value
             chineseStr=chineseStr+""+ "\n"          
     print(chineseStr)
    
def packing_part2_where_criteria(sheet):
     # 获取总行数
     nrows = sheet.nrows
     chineseStr = ""
     #循环行数
     for row in range(nrows):
         #第一行不计入
         if row !=0:
             #取第一列值
             cell_value1=sheet.cell(row,0).value
             #取第二列值
             cell_value2=sheet.cell(row,1).value
             chineseStr=chineseStr+"and "+cell_value1+"=#{"+cell_value2+"}"+ "\n"          
     print(chineseStr) 
     
def packing_part3_update_criteria(sheet):
     # 获取总行数
     nrows = sheet.nrows
     chineseStr = ""
     #循环行数
     for row in range(nrows):
         #第一行不计入
         if row !=0:
             #取第一列值
             cell_value1=sheet.cell(row,0).value
             #取第二列值
             cell_value2=sheet.cell(row,1).value
             chineseStr=chineseStr+""+cell_value1+"=#{"+cell_value2+"},"+ "\n"          
     print(chineseStr) 
     
def packing_part4_insert_criteria(sheet):
     # 获取总行数
     nrows = sheet.nrows
     chineseStr = ""
     #循环行数
     for row in range(nrows):
         #第一行不计入
         if row !=0:
             #取第一列值
             #cell_value1=sheet.cell(row,0).value
             #取第二列值
             cell_value2=sheet.cell(row,1).value
             chineseStr=chineseStr+"#{"+cell_value2+",jdbcType=VARCHAR},"+ "\n"          
     print(chineseStr)
if __name__ == '__main__':
    resolveExcel()

四、输出样例

--------------------part1-----------------------










































--------------------part2-----------------------

and BAE064=#{id}
and BAZ002=#{baz002}
and AAE140=#{insurance}
and AAC001=#{personId}
and AAC999=#{personNumber}
and AAC002=#{socialEnsureNumber}
and AAC003=#{name}
and AAC004=#{sex}
and AAC006=#{birthday}
and AAE035=#{transferDate}
and AAB001=#{unitNumber}
and AAC071=#{unitName}
and AAC105=#{transRegion}
and BAD699=#{payoffType}
and AAB299=#{tgtAgencyState}
and AAB301=#{srcAgencyState}
and AAB300=#{transferInAgencyName}
and AAA146=#{transferOutAgencyName}
and AAE009=#{agencyAccountName}
and AAC080=#{agencyBankNumber}
and AAC155=#{agencyBankName}
and AAC078=#{agencyAccount}
and AAE005=#{agencyPhone}
and AAE006=#{agencyAddress}
and AAE007=#{agencyZip}
and AAE072=#{billCode}
and BAD312=#{intoAccountStatus}
and BZZ100=#{backStatus}
and AIC083=#{personActFundTransferSum}
and AIC084=#{regionFundTransferSum}
and AIC102=#{transferFundSum}
and BAE062=#{transEventId}
and AAE013=#{remark}
and BAE969=#{payType}
and BAD435=#{failReason}
and AAE011=#{aae011}
and AAE036=#{aae036}
and AAB034=#{aab034}
and AAA027=#{aaa027}
and BZE011=#{bze011}
and BZE036=#{bze036}
and BZE034=#{bze034}

--------------------part3-----------------------

BAE064=#{id},
BAZ002=#{baz002},
AAE140=#{insurance},
AAC001=#{personId},
AAC999=#{personNumber},
AAC002=#{socialEnsureNumber},
AAC003=#{name},
AAC004=#{sex},
AAC006=#{birthday},
AAE035=#{transferDate},
AAB001=#{unitNumber},
AAC071=#{unitName},
AAC105=#{transRegion},
BAD699=#{payoffType},
AAB299=#{tgtAgencyState},
AAB301=#{srcAgencyState},
AAB300=#{transferInAgencyName},
AAA146=#{transferOutAgencyName},
AAE009=#{agencyAccountName},
AAC080=#{agencyBankNumber},
AAC155=#{agencyBankName},
AAC078=#{agencyAccount},
AAE005=#{agencyPhone},
AAE006=#{agencyAddress},
AAE007=#{agencyZip},
AAE072=#{billCode},
BAD312=#{intoAccountStatus},
BZZ100=#{backStatus},
AIC083=#{personActFundTransferSum},
AIC084=#{regionFundTransferSum},
AIC102=#{transferFundSum},
BAE062=#{transEventId},
AAE013=#{remark},
BAE969=#{payType},
BAD435=#{failReason},
AAE011=#{aae011},
AAE036=#{aae036},
AAB034=#{aab034},
AAA027=#{aaa027},
BZE011=#{bze011},
BZE036=#{bze036},
BZE034=#{bze034},

--------------------part4-----------------------

#{id,jdbcType=VARCHAR},
#{baz002,jdbcType=VARCHAR},
#{insurance,jdbcType=VARCHAR},
#{personId,jdbcType=VARCHAR},
#{personNumber,jdbcType=VARCHAR},
#{socialEnsureNumber,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{birthday,jdbcType=VARCHAR},
#{transferDate,jdbcType=VARCHAR},
#{unitNumber,jdbcType=VARCHAR},
#{unitName,jdbcType=VARCHAR},
#{transRegion,jdbcType=VARCHAR},
#{payoffType,jdbcType=VARCHAR},
#{tgtAgencyState,jdbcType=VARCHAR},
#{srcAgencyState,jdbcType=VARCHAR},
#{transferInAgencyName,jdbcType=VARCHAR},
#{transferOutAgencyName,jdbcType=VARCHAR},
#{agencyAccountName,jdbcType=VARCHAR},
#{agencyBankNumber,jdbcType=VARCHAR},
#{agencyBankName,jdbcType=VARCHAR},
#{agencyAccount,jdbcType=VARCHAR},
#{agencyPhone,jdbcType=VARCHAR},
#{agencyAddress,jdbcType=VARCHAR},
#{agencyZip,jdbcType=VARCHAR},
#{billCode,jdbcType=VARCHAR},
#{intoAccountStatus,jdbcType=VARCHAR},
#{backStatus,jdbcType=VARCHAR},
#{personActFundTransferSum,jdbcType=VARCHAR},
#{regionFundTransferSum,jdbcType=VARCHAR},
#{transferFundSum,jdbcType=VARCHAR},
#{transEventId,jdbcType=VARCHAR},
#{remark,jdbcType=VARCHAR},
#{payType,jdbcType=VARCHAR},
#{failReason,jdbcType=VARCHAR},
#{aae011,jdbcType=VARCHAR},
#{aae036,jdbcType=VARCHAR},
#{aab034,jdbcType=VARCHAR},
#{aaa027,jdbcType=VARCHAR},
#{bze011,jdbcType=VARCHAR},
#{bze036,jdbcType=VARCHAR},
#{bze034,jdbcType=VARCHAR},

你可能感兴趣的:(使用xlrd读取excel)