Python通过循环

时间:2016-07-26 14:14:00

标签: python xlwings

我目前正在使用xlwings在microsoft excel中显示图形及其对应的值。我有5个图表及其坐标(在一个数组中),我已经成功地通过循环打印。不幸的是,它们出现的列必须是硬编码的,并且如果我要添加更多的绘图会受到影响,所以我将我的代码更改为:

for i in range(1, 6):
    Columns = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S"]

    Range(Columns[0] + str(1)).value = list(zip(Xvalues))
    Range(Columns[1] + str(1)).value = list(zip(Yvalues))

目前,它将采用第一个绘图并在A列(“A1”)中垂直打印x坐标,然后在B列(“B1”)中垂直打印y坐标,然后继续。

我的问题是如何在循环中增加Columns []的索引,以便我的下一个值是Columns [3]和Columns [4]?

1 个答案:

答案 0 :(得分:0)

您可以使用另一个变量来跟踪列索引。在这种情况下j

Columns = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S"]
j = 1
for j,i in enumerate(range(1, 5)):    
    Range(Columns[j] + str(1)).value = list(zip(Xvalues))
    Range(Columns[j+1] + str(1)).value = list(zip(Yvalues))
    j += 1

此外,您不应声明循环中的列列表。