Python: modify the Excel data instance code

Source: Internet
Author: User
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 ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.