最近在學習 xlwings,參考學習的網址:https://www.jianshu.com/p/b534e0d465f7 寫得很棒,學到了很多。
在新建sheet表單, 發現一個問題。
import xlwings as xw
def creat_sheet(path, sheet=None, before=None):
'''
:param path: excel file path
:param sheet: the name of which creating the sheet, could be None or the name of sheet
:param before: the sheet before which the new sheet is added, could be int or the existing sheet name
:return:
'''
try:
wb = xw.Book(path)
if isinstance(before, int):
before = before + 1
sheet_obj = xw.sheets.add(name=sheet, before=before) # 這一行 !!!
wb.save()
return sheet_obj.name
except Exception as e:
raise e
上面的代碼 在office excel里可以運行成功, 但是在wps excel里就會報錯。
仔細查看了第一行的網址教程,發現沒有什么問題。
在一個旁觀者的驚訝聲中,發現了原因! (果然旁觀者清)
應該是 sheet_obj = wb.sheets.add(name=sheet, before=before) # 這一行 !!!
修改之后, Office和Wps 的excel 都可以運行成功。
我不知道原先的代碼 為什么可以在office excel里運行成功。。。 office 和 wps 的底層處理不一樣么??? (不懂呀~)
歡迎交流。