打开已经存在的excel,取出数据进行处理后,再次写入该表格

# -*- coding: utf-8 -*-
import xlrd
from xlutils.copy import copy

'''打开已经存在的excel,取出数据进行处理后,再次写入该表格'''


excel_file = "D:\LAB\Study\Python\writeexcel\count.xls"

# open old excel (formatting_info=True 保留原格式)
oldworkbook = xlrd.open_workbook(excel_file, formatting_info=True)
print oldworkbook
sheet = oldworkbook.sheet_by_index(0)

# get features
va = sheet.col_values(10)
print va

# '处理数据' 此处省略

# copy a workbook
newworkbook = copy(oldworkbook)
newsheet = newworkbook.get_sheet(0)

# write features
for f_id in range(0, len(va)):
print f_id
newsheet.write(f_id, 11, va[f_id])

# save属性是workbook的,不是sheet的
newworkbook.save(excel_file)



注意写入的时候,用sheet要用newsheet,这个sheet有写入的功能,不能用oldsheet,这个只有读取功能。


转载于:https://www.cnblogs.com/azure-west/p/6876048.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值