-3

This is probably super simple, but i am new to python. I wrote some code to insert a number into a certain row and column in excel. That gives me a value in another cell. I would like to iterate, by inserting -1000, then -950, then -900 up to +1000. And for every increment i would like to print the value. How is this possible?

THis is my code so far

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)

This way it works by creating a new spreadsheet, where cell N25 has the value 1000, and i can read the result from that manually. i would like python to print all values and all results for me. How can i do this?

  • https://docs.python.org/3/tutorial/controlflow.html#for-statements – 9769953 Jun 01 '22 at 08:16
  • Can you elaborate? i am new to coding, so im having a hard time grasping this. But thanks for the link. – Mandsberg1 Jun 01 '22 at 08:20
  • 1
    Elaborate? It's just a (the) Python tutorial, which may be useful to work through if you're new to Python. If you don't know for loops, that means you are new to programming in general, and you should take your time to learn the basics of Python to get going. – 9769953 Jun 01 '22 at 08:33
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 01 '22 at 11:43

2 Answers2

0

As far as I understood you're trying to run an Excel macro several times, inserting values from -1000 to +1000 with step of 50 using a Python script, then get the result for each iteration, taken from a different cell of the sheet.

If this is your case openpyxl is not able to do that, as stated in this post:

openpyxl how to read formula result after editing input data on the sheet? data_only=True gives me a "None" result

Matteo
  • 21
  • 5
0

To anyone interested, i solved it with this code

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 is the cell to enter info. AV24 is the cell to print.