本文摘自php中文网,作者不言,侵删。
下面为大家分享一篇python写入已存在的excel数据实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧python可以使用xlrd读excel,使用xlwt写excel,但是如果要把数据写入已存在的excel,需要另外一个库xlutils配合使用.
大概思路:
1、用xlrd.open_workbook打开已有的xsl文件
注意添加参数formatting_info=True,得以保存之前数据的格式
2、然后用,from xlutils.copy import copy;,之后的copy去从打开的xlrd的Book变量中,拷贝出一份,成为新的xlwt的Workbook变量
3、然后对于xlwt的Workbook变量,就是正常的:
通过get_sheet去获得对应的sheet,拿到sheet变量后,就可以往sheet中,写入新的数据
4、写完新数据后,最终save保存
源码例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import xlrd
import os
from xlutils. copy import copy
from xlwt import Style
def writeExcel(row, col, str, styl=Style.default_style):
rb = xlrd.open_workbook(file, formatting_info=True)
wb = copy (rb)
ws = wb.get_sheet(0)
ws.write(row, col, str, styl)
wb.save(file)
style = xlwt.easyxf( 'font:height 240, color-index red, bold on;align: wrap on, vert centre, horiz center' );
writeExcel(1, 1, 'hello world' , style)
|
阅读剩余部分
相关阅读 >>
Python常用方法和技巧汇总
Python 日志增量抓取实现方法
Python中class怎么用
Python使用windows api创建窗口示例
Python for语句的执行过程是什么
详解Python的局部变量和全局变量使用难点
Python如何提高运行速度
Python怎么安装?(教程图解)
Python怎么去重
Python深浅拷贝区别
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python写入已存在的excel数据实例