用python读取excel

来源:互联网 发布:java中什么是构造器 编辑:程序博客网 时间:2024/05/01 04:49
读取xls文件:import xlrd
获取xls文件
workbook = xlrd.open_workbook('xxx.xls')
获取表单数量
workbook.nsheets
获取一个表单
workbook.sheets()[i]
workbook.sheet_by_index(i)
workbook.sheet_by_name('sheet1')
访问数据
sheet.cell(i,j).value
sheet.row(i)[j].value
sheet.col(j)[i].value


写xls文件: import xlwt
book = xlwt.Workbook(encoding = 'utf-8',style_compression = 0)
sheet = book.add_sheet('dada',cell_overwrite_ok = True)
sheet.write(i,j,content)
book.save(path)
写入xls文件设置格式
设置背景色:
style = xlwt.easyxf('pattern: pattern solid, fore_color yellow;')
设置加粗
style = xlwt.easyxf('font: bold 1')
设置对齐:
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_LEFT
horz_style = xlwt.XFStyle()
horz_style.alignment = alignment
---------其实不用这么复杂-------------following show the answer
xlwt.easyxf("pattern:pattern solid,fore_color yellow;font:color white;align:horizright")
设置列宽:
sheet.col(0).width = 256 * (len(key) + 1) 


自动换行:
style = xlwt.easyxf('align: wrap on')
sheet.write(0, 0, 'Firstname', style)
1 0
原创粉丝点击