打开已经存在的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,这个只有读取功能。


posted @ 2017-05-18 22:53  west_Tang风  阅读(537)  评论(0编辑  收藏  举报