python对Excel的处理

Excel读取模块 xlrd

import xlrd
data = xlrd.open_workbook("data1.xlsx")
##表
print(data.sheets())
print(data.sheet_load(0)
print(data.sheet_by_name())
print(data.sheet_by_index())
print(data.sheet_names())
print(data.nsheets) #返回excel工作表的数量

##行
sheet = data.sheet_by_index(0)
print(sheet.nrows)#sheet下的有效行数
print(sheet.row(1))
print(sheet.row(1)[2].value)
print(sheet.row_types(1))
print(sheet.row_values(1))
print(sheet.row_len(1))

##列
sheet = data.sheet_by_index(0)
print(sheet.ncols)#sheet下的有效列数
print(sheet.col(1))
print(sheet.col(1)[2].value)
print(sheet.col_types(1))
print(sheet.col_values(1))
print(sheet.col_len(1))

##单元格
sheet = data.sheet_by_index(0)
print(sheet.cell(1,2))
print(sheet.cell_type(1,2))
print(sheet.cell(1,2).ctype)#获取单元格数据类型
print(sheet.cell(1,2).value)
print(sheet.cell_value(1,2))

Extcel写入模块 xlwt

import xlwt
titlestyle = xlwt.XFStyle()

titlefont = xlwt.Font()

titlefont.name = "宋体"
titlefont.bold = True
titlefont.height = 11*20
titlefont.colour_index = 0x08 #字体颜色
titlestyle.font = titlefont

cellalign = xlwt.Alignment()
cellalign.horz = 0x02#水平对齐
cellalign.wert = 0x02#竖直对其
titlestyle.alignment = cellalign

#第一步:创建工作簿
wb = xlmt.Workbook()
#第二部:创建工作表
ws = wb.add_sheet('CNY')
#第三部:填充数据
ws.write_merge(0,1,0,5,'2019', titlestyle)
#第四部:保存
wb.save("2019-CNY.xls")


#写入元组数据
data = (("aaa", 10, 20, 50),("bbb", 30, 20, 10))
for i, item in enumerate(data):
    for j, val in enumerate(item):
        ws.write(i+2, j, val)

你可能感兴趣的:(python,开发语言)