1、安装xlwings
pip install xlwings
2、代码
import xlwings as xw
def readExcel():
# xlwings对表操作的性能测试
# 创建操作对象
app = xw.App(visible=True, add_book=False)
app.display_alerts = False # 关闭一些提示信息,可以加快运行速度。 默认为 True。
app.screen_updating = False # 更新显示工作表的内容。默认为 True。关闭它也可以提升运行速度。
# 打开已有的工作簿
wb = app.books.open(r'D:\code\python\19e5_224045578950071741_5b3b213404ed44c0afee04a2ce48515f.xlsx')
sht = wb.sheets[0]
# 读取工作表的数据
# 按行加载读取
# 得到表中有值的最大行和列
table_info = sht.used_range
nrows = table_info.last_cell.row
ncolumns = table_info.last_cell.column
#print(f'表的最大行==》{nrows},最大列==》{ncolumns}')
readData = []
#列字母计算
row = {1:"A",2:"B",3:"C",4:"D",5:"E",6:"F",7:"G",8:"H",9:"I",10:"J",11:"K",12:"L",13:"M",14:"N",15:"O",16:"P",17:"Q",18:"R",19:"S",20:"T",21:"U",22:"V",23:"W",24:"X",25:"Y",26:"Z",}
if ncolumns > 26:
c = int(ncolumns / 26)
y = int(ncolumns % 26)
if y == 0 :
c = c - 1
y = 26
column = row[c] + row[y]
else:
column = row[ncolumns]
# 按行取值,循环加入列表中
for i in range(1,nrows+1):
v = sht.range(f'A{i}:{column}{i}').options(transpose=False).value
if not v: break # 值是空的,结束循环
readData.append(v)
print(readData)
# 资源关闭
wb.close()
app.quit()
readExcel()