import xlrdexcel=xlrd.open_workbook('E:/test.xlsx')
返回值为xlrd.book.Book对象,不能修改
获取sheettable_List = excel.sheet_names() #获取Book对象中所有sheet名称,以列表方式显示table = excel.sheets[i] #通过索引获取 table = excel.sheet_by_index(i) #通过索引获取 table = excel.sheet_by_name('Sheet1') #通过表名获取
获取行数和列数: rows=table.nrows #获取行数cols=table.ncols #获取列数
读取单元格Data=table.cell(row,col).valuecell_A1 = table.row(0)[0].valuecell_A2 = table.col(1)[0].value
第一行第一列是从0开始的,注意不要丢掉 .value
获取整行或整列内容Row_values=table.row_values(i) #获取整行内容Col_values=table.col_values(i) #获取整列内容
xlwtimport xlwtfrom datetime import datetimestyle0 = xlwt.easyxf('Font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00')style1 = xlwt.easyxf(num_format_str='D-MMM-YY')wb = xlwt.Workbook()ws = wb.add_sheet('A Test Sheet')ws.write(0, 0, 1234.56, style0)ws.write(1, 0, datetime.Now(), style1)ws.write(2, 0, 1)ws.write(2, 1, 1)ws.write(2, 2, xlwt.Formula("A3+B3"))wb.save('example.xls')
xlutils官网链接 xlutiles
xlutils.copyTools for copying xlrd.Book objects to xlwt.Workbook objects.
You would start by opening the file with xlrd:
>>> from os.path import join>>> from xlrd import open_workbook>>> rb = open_workbook(join(test_files,'testall.xls'), formatting_info=True, on_demand=True)
You would then use xlutils.copy to copy the xlrd.Book object into an xlwt.Workbook object:
>>> from xlutils.copy import copy>>> wb = copy(rb)
Now that you have an xlwt.Workbook, you can modify cells and then save the changed workbook back to a file:
>>> wb.get_sheet(0).write(0,0,'changed!')>>> wb.save(join(temp_dir.path,'output.xls'))>>> temp_dir.Listdir()output.xls
总结 以上是内存溢出为你收集整理的python--xlrd/xlwt/xlutils全部内容,希望文章能够帮你解决python--xlrd/xlwt/xlutils所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)