Python 操作Excel

__VERSION__ = '1.0'
import xlrd as rd;
import xlwt as wt;
import os;


class ExcelOperation():
    def readExcel(self):        
        excelDir=os.getcwd()+"\\"+"2222.xls";
        book=rd.open_workbook(excelDir);
        print(book.nsheets);
        sheet1=book.sheets()[0];
        for i in range(sheet1.nrows):
            for j in range(sheet1.ncols):
                print(sheet1.cell(i,j).value)
         
    def writeExcel(self):
        excelDir=os.getcwd()+"\\"+"2223.xls";
        book=wt.Workbook();
        sheet1=book.add_sheet("A")
        for i in range(5):
            for j in range(4):
                sheet1.write(i,j,i+j);
        book.save(excelDir);

if __name__=="__main__":
    print("Self")
    obj=ExcelOperation();
    obj.readExcel();
    obj.writeExcel();
else:
    print("Other");
            
View Code

 

posted @ 2015-03-19 16:43  奔雷手  阅读(87)  评论(0)    收藏  举报