python Excel读写

1.读取

python Excel读写_第1张图片

 实例:

注意,导入库要导入最新版本

pip install xlrd==1.2.0

import xlrd

xlsx = xlrd.open_workbook('E:LessonCode')

table = xlsx.sheet_by_index(0)
# 通过sheet名查找:xlsx.sheet_by_name("7月下旬入库表")
# 通过索引查找:xlsx.sheet_by_index(3)
print(table.cell_value(0, 0))

# table.cell_value(1, 2)
# print(table.cell(1, 2).value)
# print(table.row(1)[2].value)



for i in range(0, xlsx.nsheets):
    table = xlsx.sheet_by_index(i)
    print(table.cell_value(0, 0))

# 获取所有sheet名字:xlsx.sheet_names()
# 获取sheet数量:xlsx.nsheets

for i in xlsx.sheet_names():
    table = xlsx.sheet_by_name(i)
    print(table.cell_value(3, 3))

2.写入

python Excel读写_第2张图片

import xlwt
new_workbook = xlwt.Workbook()
worksheet = new_workbook.add_sheet('new_test')
worksheet.write(0, 0, 'test')
new_workbook.save('d:/test.xls')

你可能感兴趣的:(python自动化,python,excel,开发语言)