from xlutils.copy import copy
import xlrd
import xlwt
tem_excel=xlrd.open_workbook('E:/pythonLesson/1.xls',formatting_info=True)
tem_sheet=tem_excel.sheet_by_index(0)
new_excel=copy(tem_excel)
new_sheet=new_excel.get_sheet(0)
style=xlwt.XFStyle()
font=xlwt.Font()
font.name='微软雅黑'
font.bold=True
font.height=360
style.font=font
borders=xlwt.Borders()
borders.top=xlwt.Borders.THIN
borders.bottom=xlwt.Borders.THIN
borders.left=xlwt.Borders.THIN
borders.right=xlwt.Borders.THIN
style.borders=borders
alignment=xlwt.Alignment()
alignment.horz=xlwt.Alignment.HORZ_CENTER
alignment.vert=xlwt.Alignment.VERT_CENTER
style.alignment=alignment
new_sheet.write(2,1,12,style)
new_sheet.write(3,1,18,style)
new_sheet.write(4,1,19,style)
new_sheet.write(5,1,15,style)
new_excel.save('E:/pythonLesson/1.xls')
Excel的读取和写入
import xlrd,xlwt
xlsx=xlrd.open_workbook("path")
table=xlsx.sheet_by_index(0)
rows_count=tables.nrows
for x in xrange(1,rows_count):
city=table.cell(x,1).value
unit_saled=table.cell(x,4).value
print(table.cell_value(1,2))
print(table.cell(1,2).value)
print(table.row(1)[2].value)
new_Workbook=xlwt.Workbook()
worksheet=new_Workbook.add_sheet('new_test')
idx=1
for country,saled in result:
worksheet.write(idx,0,contry)
worksheet.write(idx,1,saled)
idx+=1
worksheet.write(0,0,'test'