如何在excel中使用python进行迭代



这可能超级简单,但我是python新手。我写了一些代码,在excel中插入一个数字到特定的行和列。这就得到了另一个单元格的值。我想迭代,插入-1000,然后-950,然后-900直到+1000。对于每一个增量,我想打印值。这怎么可能呢?

这是我到目前为止的代码

import xlwings as xw 
import pandas as pd
import matplotlib.pyplot as plt
#load the excel file

wb = xw.Book("Datasets/Sektion_20111.xlsm")
#Sheet
sht = wb.sheets["Beregning"]
#dataframe
#Cell with normal force
sht.range("N25").value = (500)
#Print cell with nedre grænse, brudmoment
print(sht["AV24"].value)

这种方式通过创建一个新的电子表格来工作,其中单元格N25的值为1000,我可以手动读取结果。我希望python为我打印所有值和所有结果。我该怎么做呢?

据我所知,您正在尝试运行Excel宏几次,使用Python脚本插入从-1000到+1000的值,步骤为50,然后获得每次迭代的结果,从工作表的不同单元格中获取。

如果这是你的情况,openpyxl无法做到这一点,如本文所述:

openpyxl如何在编辑输入数据后读取公式结果?data_only=True给我一个"none";结果

对于感兴趣的人,我用下面的代码解决了这个问题

import xlwings as xw 
#load the excel file
wb = xw.Book("Datasets/Sektion_20111.xlsm")
#Sheet
sht = wb.sheets["Beregning"]
#for loop
x = range(-100, 100, 50)
for i in x:
#Cell with normal force
sht.range("N25").value = i
print(sht["AV24"].value)   

N25是输入info的单元格。AV24是要打印的单元格。

最新更新