Python modifies instance code for Excel data
- 2020-04-02 09:53:46
- OfStack
After the previous article on how to read and write Excel data in Python, today I'll show you how to modify Excel data in Python. The xlutils module is required. Download address to https://pypi.python.org/pypi/xlutils. After downloading, simply execute the python setup.py install command to install.
The specific use code is as follows:
#-*-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 In the column
url_index = 12
#domain In the column
domain_index = 11
#malinfo In the column
malinfo_index = 9
file_path = "C:\Users\***\Desktop\20130514.xls"
#formatting_info=True Save the format of the previous data
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" To obtain a malicious URL , write to the configuration file and download the malicious executable. "
#r_sheet.nrows As the total number of rows
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' is ')
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")