当前位置: 首页 > 工具软件 > Python Excel > 使用案例 >

python excel文件的读写修改保存

汤弘文
2023-12-01

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]

参考
http://blog.csdn.net/canhui_wang/article/details/78662742

 类似资料: