【Python】Excel操作-2 (07版本以下Excel操作,其实不怎么用了,麻蛋,预习了2天课间才发现,还说怎么跟老师讲的不一样)

#保存修改Excel
import xlrd
from xlutils.copy import copy
 
#打开Excel文档并将内容读取到内存
readbook=xlrd.open_workbook("e:\test4\s.xls")
 
#将Excel内容copy一份
copybook=copy(readbook)
 
#遍历Excel文档中的每一个工作表,进行下面的处理
for i in range(len(readbook.sheets())):
 
    #获得copy的Excel中的表i
    sheet=copybook.get_sheet(i)
 
    #获取原Excel文档中的表i
    readsheet=readsheet.sheet_by_index(i)
    for row in range(readsheet.nrows):
 
    #对表i中的单元格数据做如下处理
        for col in range(readsheet.ncols):
            cell=readsheet.cell(row,cell).value
            print "type is:",type(cell),cell
            if type(cell) in (str,unicode):
            #判断一下读取出来的单元格数据格式,如果为str和unicode的字符串,就调用字符串的upper函数,将小写改成大写。
            res=cell.upper() #把小写改成大写
            sheet.write(row, col, res)#将修改的内容写入拷贝的excel中
            #sheet.write(row, col, '中文'.decode('GBK'))
 
#将拷贝的excel保存为新文件,或覆盖原来的excel文件
copybook.save("e:\test4\s2.xls")
print "************ end **************"
原文地址:https://www.cnblogs.com/jingsheng99/p/8886809.html