xlrd

1、导入模块
import xlrd
2、打开Excel文件读取数据
data = xlrd.open_workbook('excelFile.xls')
3、使用技巧
#获取一个工作表
table = data.sheets()[0]          #通过索引顺序获取
table = data.sheet_by_index(0) #通过索引顺序获取
table = data.sheet_by_name(u'Sheet1')#通过名称获取
#获取整行和整列的值(数组)
table.row_values(1, start_colx=0, end_colx=None)
Out[10]: ['', '公司相关材料清单,此表中的材料请提供加盖公章的复印件扫描件', '']
table.col_values(1, start_rowx=0, end_rowx=None)
Out[11]: 
['',
 '公司相关材料清单,此表中的材料请提供加盖公章的复印件扫描件',
 '1.营业执照正副本',
 '3.企业法人身份证',
 '5.最新一期审计报告',
 '7.风控内控制度文件',
 '',
 '',
 '',
 '产品层面材料清单,此表中的材料请以Excel形式提供',
 '1.旗下各个策略代表性产品自成立以来的每日或者每周净值序列(最新净值至少更新到2017年4月底)',
 '2.旗下各个策略代表性产品持仓数据(可选)',
 '']
 # 获取行数和列数
nrows = table.nrows
ncols = table.ncols
#循环行列表数据
for i in range(nrows ):
    print table.row_values(i)
#单元格
cell_A1 = table.cell(0,0).value
cell_A1
Out[17]: ''
cell_A1 = table.cell(3,2).value
cell_A1
Out[19]: '4.私募投资基金管理人登记证明'
#使用行列索引
cell_A1 = table.row(3)[2].value
cell_A1
Out[21]: '4.私募投资基金管理人登记证明'
cell_A1 = table.col(2)[3].value
cell_A1
Out[23]: '4.私募投资基金管理人登记证明'
# 简单的写入
row, col = 0, 0
ctype=1; value="值"
xf =0
table.put_cell(row, col, ctype, value, xf)
table.cell(0,0)
Out[28]: text:'值'

table
Out[29]: 0x24e1dae5c88>
#get_rows()
#Returns a generator for iterating through each row.
#返回每行的generator
for x in table.get_rows():
    print(x)

[text:'值', empty:'', empty:'']
[empty:'', text:'公司相关材料清单,此表中的材料请提供加盖公章的复印件扫描件', empty:'']
[empty:'', text:'1.营业执照正副本', text:'2.组织机构代码证']
[empty:'', text:'3.企业法人身份证', text:'4.私募投资基金管理人登记证明']
[empty:'', text:'5.最新一期审计报告', text:'6.近三年财务报表']
[empty:'', text:'7.风控内控制度文件', empty:'']
[empty:'', empty:'', empty:'']
[empty:'', empty:'', empty:'']
[empty:'', empty:'', empty:'']
[empty:'', text:'产品层面材料清单,此表中的材料请以Excel形式提供', empty:'']
[empty:'', text:'1.旗下各个策略代表性产品自成立以来的每日或者每周净值序列(最新净值至少更新到2017年4月底)', empty:'']
[empty:'', text:'2.旗下各个策略代表性产品持仓数据(可选)', empty:'']
[text:'§:请勿删除该符号,并确保填写内容在该符号上方', empty:'', empty:'']

根据索引

#-*-coding:utf-8-*-
import xlrd
def open_excel(file='file.xls'):
    try:
        data=xlrd.open_workbook(file)
        return data
    except Exception as e:
        print(str(e))

# 根据索引获取Excel表格中的数据参数:file:Excel文件路径colname_index:表头列名所在行的所以,by_index:表的索引

def excel_table_byindex(file=None,colname_index=0,by_index=0):
    data = open_excel(file)
    table = data.sheets()[by_index]
    nrows = table.nrows  # 行数
    ncols = table.ncols  # 列数
    colnames = table.row_values(colname_index)  # 某一行数据 #提取列名['列一', '列二', '列三']
    # row_values(行值:0行的值既是列名)
    res_list = []
    for rownum in range(1, nrows):
        row = table.row_values(rownum)  # 一行行遍历,list类型
        if row:
            app = {}
            for i in range(len(colnames)):
                app[colnames[i]] = row[i]
                res_list.append(app)
    return res_list  # dict

if __name__ == "__main__":
    excel_table_byindex(file='C:\\Users\\chaoLi\\Downloads\\汇升尽调模板.xlsx')

根据sheet名

#-*-coding:utf-8-*-
import xlrd
def open_excel(file='file.xls'):
    try:
        data=xlrd.open_workbook(file)
        return data
    except Exception as e:
        print(str(e))

# 根据名称获取Excel表格中的数据参数:file:Excel文件路径colname_index:表头列名所在行的所以,by_name:Sheet1名称
def excel_table_byname(file='file.xls',colname_index=0,by_name=u'0.项目概要与填写说明'):
    data = open_excel(file)
    table=data.sheet_by_name(by_name)  # data.sheet_names() 获取sheet名
    nrows=table.nrows#行数
    colnames=table.row_values(colname_index)#某一行数据
    res_list=[]
    for rownum in range(1,nrows):
        row=table.row_values(rownum)
        if row:
            app={}
            for i in range(len(colnames)):
                app[colnames[i]]=row[i]
                res_list.append(app)
    return res_list

def main():
    tables=excel_table_byname(file="C:\\Users\\chaoLi\\Downloads\\汇升尽调模板.xlsx")
    for row in tables:
        print(row)  # {'列名':'下面某行的值'}

if __name__ == "__main__":
    main()

你可能感兴趣的:(python及后端,-----Quixote)