python xlwt_python – 使用xlwt写入现有工作簿

这篇博客分享了如何使用Python库xlutils和xlrd操作Excel,通过遍历指定工作簿的行,如果满足特定条件(如年龄为3),则在对应单元格写入'ComboI3-4yearold'。最终实现对Excel文件的批量更新并保存为新文件。
摘要由CSDN通过智能技术生成

这是我最近用来做的一些示例代码.

它打开一个工作簿,沿着行向下,如果满足条件,它会在行中写入一些数据.最后它保存修改后的文件.

from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils

from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd

from xlwt import easyxf # http://pypi.python.org/pypi/xlwt

START_ROW = 297 # 0 based (subtract 1 from excel row number)

col_age_november = 1

col_summer1 = 2

col_fall1 = 3

rb = open_workbook(file_path,formatting_info=True)

r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file

wb = copy(rb) # a writable copy (I can't read values out of this, only write to it)

w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy

for row_index in range(START_ROW, r_sheet.nrows):

age_nov = r_sheet.cell(row_index, col_age_november).value

if age_nov == 3:

#If 3, then Combo I 3-4 year old for both summer1 and fall1

w_sheet.write(row_index, col_summer1, 'Combo I 3-4 year old')

w_sheet.write(row_index, col_fall1, 'Combo I 3-4 year old')

wb.save(file_path + '.out' + os.path.splitext(file_path)[-1])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值