我想在Python中创建一个列循环

问题描述 投票:-2回答:1

我想使用openpyxl在Excel中创建一个循环,一切正常,更新我的列,但保存时我只重写最后一列的最后一个值。

from openpyxl import Workbook
x = 1
while True:
 wb = Workbook()
 ws = wb.active
 ws['A' + str(x)] = 42
 wb.save("test.xlsx")
 y = input("dasdsa :")
 if y == "ESC":
   break
 else:
   x += 1
   continue'
python excel loops openpyxl
1个回答
0
投票

wb.save移动到你的if y街区

from openpyxl import Workbook 
x = 1 
wb = Workbook() 
while True: 
  ws = wb.active 
  ws['A' + str(x)] = 42 
  y = input("dasdsa :") 
  if y == "ESC": 
    wb.save("test.xlsx") 
    break 
  else: 
    x += 1 
    continue 
© www.soinside.com 2019 - 2024. All rights reserved.