注意:保存文件时,文件要先关闭掉,不然会报PermissionError
# -*- coding: UTF-8 -*-
import xlrd
from xlutils.copy import copy
#打开文件,xlrd.open_workbook()方法返回xlrd.Book类型,是只读的,不能对其进行操作,formatting_info=True:保留Excel的原格式
file = xlrd.open_workbook("D:\\版本测试\\v10.9.4\\版本计划.xls",formatting_info=True)
print(file.sheets())
sheet1 = file.sheets()[0]
sheet2 = file.sheet_by_index(0)
sheet3 = file.sheet_by_name('Sheet2')
print(sheet1)
print(sheet2)
print(sheet3)
#获取当前文件的表
shxrange = range(file.nsheets)
try:
#获取名字为“Sheet2"的sheet页
sh = file.sheet_by_name('Sheet2')
print(sh)
except:
print("no sheet in %s named 'Sheet2'",format("D:\\版本测试\\v10.9.4\\版本计划.xlsx"))
#获取行数
nrows = sh.nrows
#获取列数
ncols = sh.ncols
#打印表格的行数和列数
print("nrows{0},ncols{1}".format(nrows,ncols))
#新建一个空列表
row_list = []
for i in range(0,nrows):
row_data = sh.row_v