用python套用excel模板

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
#字体的高度等于  字号*20
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
#读取excel数据
xlsx=xlrd.open_workbook("path")
#找到sheet可以通过  表明或表序号
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)
#写入excel
#新建一个工作簿
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'
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值