import xlwings
"""
visible用来设置程序是否可见,True表示可见(默认),Flase不可见。
add_book用来设置是否自动创建工作簿,True表示自动创建(默认),False不创建
"""
path_file = "aa.xlsx"
app = xlwings.App(visible=True,add_book=False)
wb = app.books.open(path_file) #打开指定的excel文件
sht1 = wb.sheets['Sheet1'] #指定工作表的名称
# sht1 = xw.sheets.active
#读值
a1 = sht1.range("A1").value #读取指定单元格 A1:A列第一行
print("a1",a1)
#写值
sht1.range("B4").value = "python"
#读取行和列数
print(f"行数为:{sht1.used_range.shape[0]}列数为:{sht1.used_range.shape[1]}")
wb.save()
wb.close()
app.quit()
#错误:
(-2146827864, 'OLE error 0x800a01a8', None, None),不应该在读取文件或操作文件之前关闭execl表
python xlwings的使用
于 2023-01-06 16:08:43 首次发布