In the previous article, I described how to read and write Excel data in Python, and today I'll explain how to modify Excel data in Python. Need to use the Xlutils module. The download address is https://pypi.python.org/pypi/xlutils. After downloading, execute the Python setup.py install command to install it.
The specific code is as follows:
Copy Code code 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所在列
Url_index = 12
#domain所在列
Domain_index = 11
#malinfo所在列
Malinfo_index = 9
File_path = "C:\\users\\***\\desktop\\20130514.xls"
#formatting_info =true The 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 the malicious URL, writes the configuration file, downloads the malicious executable program." "
#r_sheet. Nrows is 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 ' yes ')
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")