Python修改Excel数据的实例代码

yipeiwu_com5年前Python基础

在前面的文章中介绍了如何用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 sys,os,time模块的使用(包括时间格式的各种转换)

sys模块 sys.argv: 实现从程序外部向程序传递参数。 位置参数argv[0]代表py文件本身,运行方法 python xx.py 参数1,参数2 。。 self = s...

Python处理文本换行符实例代码

Python处理文本换行符实例代码

本文研究的主要是Python处理文本换行符的相关内容,具体如下。 源文件每行后面都有回车,所以用下面输出时,中间会多了一行 try: with open("F:\\hjt.txt...

python实现将pvr格式转换成pvr.ccz的方法

本文实例讲述了python实现将pvr格式转换成pvr.ccz的方法。分享给大家供大家参考。具体实现方法如下: import zlib import struct import sy...

Python程序中使用SQLAlchemy时出现乱码的解决方案

今天对clubot进行了升级, 但是导入数据后中文乱码, 一开是找资料说是在创建引擎的时候添加编码信息: engine = create_engine("mysql://root:@...

python中format()函数的简单使用教程

python中format()函数的简单使用教程

先给大家介绍下python中format函数,在文章下面给大家介绍python.format()函数的简单使用 ---恢复内容开始--- python中format函数用于字符串的格式化...