python自带读写excel_Python读写Excel

#导入库

importxlrd,xlwtfrom xlutils.copy importcopy#打开旧工作簿

old_file = r'D:/12 用Python自动办公,做职场高手(完结)/01.文件/【12.20更新课程代码】用Python自动办公做职场高手/CourseCode/Chapter1/S1-1-2/LessonCode/日统计.xls'old_book= xlrd.open_workbook(old_file,formatting_info=True)

old_sheet=old_book.sheet_by_index(0)#复制工作簿

new_book =copy(old_book)

new_sheet=new_book.get_sheet(0)

#输入样式

style =xlwt.XFStyle()

font=xlwt.Font()

font.name= '微软雅黑'font.bold=True

font.height= 360style.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

aligment=xlwt.Alignment()

aligment.horz=xlwt.Alignment.HORZ_LEFT

aligment.vert=xlwt.Alignment.VERT_TOP

style.alignment=aligment

style_red=xlwt.XFStyle()

font_red=xlwt.Font()

font_red .name= '微软雅黑'font_red.colour_index= 2font_red .bold=True

font_red .height= 360style_red .font=font_red

borders_red=xlwt.Borders()

borders_red .top=xlwt.Borders.THIN

borders_red .bottom=xlwt.Borders.THIN

borders_red .left=xlwt.Borders.THIN

borders_red .right=xlwt.Borders.THIN

style_red .borders=borders_red

aligment_red=xlwt.Alignment()

aligment_red .horz=xlwt.Alignment.HORZ_LEFT

aligment_red .vert=xlwt.Alignment.VERT_TOP

style_red .alignment=aligment_red

style_18=xlwt.XFStyle()

font_18=xlwt.Font()

font_18 .name= '隶书'font_18 .bold=True

font_18 .height= 360style_18 .font=font_18

borders_18=xlwt.Borders()

borders_18 .top=xlwt.Borders.THIN

borders_18 .bottom=xlwt.Borders.THIN

borders_18 .left=xlwt.Borders.THIN

borders_18 .right=xlwt.Borders.THIN

style_18 .borders=borders_18

aligment_18=xlwt.Alignment()

aligment_18 .horz=xlwt.Alignment.HORZ_LEFT

aligment_18 .vert=xlwt.Alignment.VERT_TOP

style_18 .alignment=aligment_18

style_20=xlwt.XFStyle()

font_20=xlwt.Font()

font_20 .name= '隶书'font_20 .bold=True

font_20 .height= 400style_20 .font=font_20

borders_20=xlwt.Borders()

borders_20 .top=xlwt.Borders.THIN

borders_20 .bottom=xlwt.Borders.THIN

borders_20 .left=xlwt.Borders.THIN

borders_20 .right=xlwt.Borders.THIN

style_20 .borders=borders_20

aligment_20=xlwt.Alignment()

aligment_20 .horz=xlwt.Alignment.HORZ_LEFT

aligment_20 .vert=xlwt.Alignment.VERT_TOP

style_20 .alignment=aligment_20

stylex= lambda x: style_red if x > 10 elsestyle#输入数字

zs_n = int(input("请输入张三:"))

ls_n= int(input("请输入ls:"))

ws_n= int(input("请输入ws:"))

zl_n= int(input("请输入zl:"))#填入数据

new_sheet.write(0,0,old_sheet.cell_value(0,0),style_20)

new_sheet.write(1,1,old_sheet.cell_value(1,1),style_18)

new_sheet.write(1, 0, old_sheet.cell_value(1, 0), style_18)"""new_sheet.write(1, 1, old_sheet.cell_value(1, 1), style_18)

new_sheet.write(2, 0, old_sheet.cell_value(2, 0), style_18)

new_sheet.write(3, 0, old_sheet.cell_value(3, 0), style_18)

new_sheet.write(4, 0, old_sheet.cell_value(4, 0), style_18)

new_sheet.write(5, 0, old_sheet.cell_value(5, 0), style_18)"""

for i in range(1,6):

new_sheet.write(i,0,old_sheet.cell_value(i,0),style_18)

new_sheet.write(2,1,zs_n,stylex(zs_n))

new_sheet.write(3,1,ls_n,stylex(ls_n))

new_sheet.write(4,1,ws_n,stylex(ws_n))

new_sheet.write(5,1,zl_n,stylex(zl_n))

new_file= r'C:/Users/Administrator/Desktop/新建文件夹/1_2_p2.xls'new_book.save(new_file)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值