Python to modify Excel data. The previous article introduced how to use Python to read and write Excel data. today I will introduce how to use Python to modify Excel data. The xlutils module is required. It is https://pypi.python.org/pypi/xlutils. Run the python setup. py install command to install python.
The code is as follows:
The 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 column
Url_index = 12
# Domain column
Domain_index = 11
# Malinfo column
Malinfo_index = 9
File_path = "C: \ Users \ *** \ Desktop \ 20130514.xls"
# Formatting_info = True format of the data before saving
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 "gets malicious URLs, writes them to the configuration file, and downloads malicious executable programs. "
# R_sheet.nrows indicates 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 ")