Python修改Excel数据的实例代码
2015-12-31 22:11
1031 查看
http://www.4byte.cn/learning/1023.html
在前面的文章中介绍了如何用Python读写Excel数据,今天再介绍一下如何用Python修改Excel数据。需要用到xlutils模块。下载地址为https://pypi.python.org/pypi/xlutils。下载后执行python
setup.py install命令进行安装即可。
具体使用代码如下
在前面的文章中介绍了如何用Python读写Excel数据,今天再介绍一下如何用Python修改Excel数据。需要用到xlutils模块。下载地址为https://pypi.python.org/pypi/xlutils。下载后执行python
setup.py install命令进行安装即可。
具体使用代码如下
#-*-coding:utf-8-*- 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 import sys reload(sys) sys.setdefaultencoding('utf-8') # 0 based (subtract 1 from excel row number) START_ROW = 404 ismal_index = 2 #url所在列 url_index = 12 #domain所在列 domain_index = 11 #malinfo所在列 malinfo_index = 9 file_path = "C:\\Users\\***\\Desktop\\20130514.xls" #formatting_info=True保存之前数据的格式 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 malurl = '''http://xbox.ooqqxx.com/res/ext.jar http://xbox.ooqqxx.com/res/stat.jar http://xbox.ooqqxx.com/pages/v.html http://xbox.ooqqxx.com/pages/extv.html http://xbox.ooqqxx.com/pages/r.html''' domain_info = "http://xbox.ooqqxx.com" malinfo = u"获取恶意URL,写入配置文件中,下载恶意可执行程序。" #r_sheet.nrows为总行数 for row_index in range(START_ROW, r_sheet.nrows): #xlsvalue = r_sheet.cell(row_index, col_age_november).value w_sheet.write(row_index, ismal_index, u'是') w_sheet.write(row_index, url_index, malurl) w_sheet.write(row_index, domain_index, domain_info) w_sheet.write(row_index, malinfo_index, malinfo) #wb.save(file_path + '.out' + os.path.splitext(file_path)[-1]) wb.save("C:\\Users\\***\\Desktop\\2013.xls")
相关文章推荐
- python往某一行行尾追增内容
- python使用mysql数据库
- python学习(一)pycharm安装配置篇
- python 的打包及安装 distutils
- python删除某行
- Python 代码实现 JPG图片转PGM图片
- Python中用startswith()函数判断字符串开头
- python替换文件指定字符串
- 在Python中实现PageFactory模式
- Merge Sort(归并排序)的JAVA与Python实现
- python编码问题、乱码问题
- Python 2.4 递归函数
- 简单实现python爬虫功能
- python 中文乱码问题
- 管理后台-后端-Python篇
- python实现颜色空间转换程序(Tkinter)
- python 解析xml 文件: Element Tree 方式
- 基于python的Tkinter实现一个简易计算器
- 简明Python学习笔记之二--第一个Hello word!
- The Hacker's Guide To Python 单元测试