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>
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:'']
根据索引
import xlrd
def open_excel(file='file.xls'):
try:
data=xlrd.open_workbook(file)
return data
except Exception as e:
print(str(e))
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)
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
if __name__ == "__main__":
excel_table_byindex(file='C:\\Users\\chaoLi\\Downloads\\汇升尽调模板.xlsx')
根据sheet名
import xlrd
def open_excel(file='file.xls'):
try:
data=xlrd.open_workbook(file)
return data
except Exception as e:
print(str(e))
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)
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()