請教想要利用 while 和 try 做一個迴圈重複存取 xlsm 裡,但是資料是累加不會覆蓋如下:
分開 while 和 try 都可以單獨運行,但是合併使用跑不動,請高手賜教,謝謝
from openpyxl import Workbook
from openpyxl import load_workbook
t=0
while t <= 3:
filename = "myfile.xlsx"
d1 = 'ID:001'
d2 = 'ID:002'
d3 = 'ID:003'
new_row = [(d1), (d2), (d3)]
try:
wb = load_workbook(filename)
ws = wb.worksheets[0]
except FileNotFoundError:
headers_column = ['Header 1', 'Header 2', 'Header 3']
wb = Workbook()
ws = wb.active
ws.append(headers_row)
ws.append(new_row)
wb.save(filename)
wb.save.close()
t = t + 1