1. 程式人生 > >python excel檔案的讀寫修改儲存

python excel檔案的讀寫修改儲存

1. 測試程式碼

#coding=utf-8

import xlwt
import xlrd

# write excel file
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet('sheet1',cell_overwrite_ok=True)
#sheet2 = workbook.add_sheet('sheet2',cell_overwrite_ok=True)

sheet1.write(0,0,'this should overwrite1')
sheet1.write(0,1,'aaaaaaaaaaaa')
#sheet2.write(0,0,'this should overwrite2')
#sheet2.write(1,2,'bbbbbbbbbbbbb')

# save excel file
workbook.save('C:\\Users\\32065\\Desktop\\Net\\simulation\\excel\\test.xls')
print 'create excel file successfully!'

# read the excel file
data = xlrd.open_workbook('C:\\Users\\32065\\Desktop\\Net\\simulation\\excel\\test.xls')
table = data.sheets()[0] # 開啟第一張表
nrows = table.nrows # 獲取表的行數
for i in range(nrows): # 迴圈逐行列印
    #if i == 0: # 跳過第一行
    #    continue
    print table.row_values(i)[0:2]
    # print table.row_values(i)[1:2]