Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing multiple Excel Values with PY #32

Open
ahagmann83 opened this issue Sep 16, 2023 · 8 comments
Open

Writing multiple Excel Values with PY #32

ahagmann83 opened this issue Sep 16, 2023 · 8 comments

Comments

@ahagmann83
Copy link

Ok, I can read Excel Cell Values into my Python code with xl. But what about writing? (How) can I write values back into (multiple) Excel Cells?

@keyur32
Copy link
Collaborator

keyur32 commented Sep 20, 2023

Python formulas only return data within the cell they were entered on. https://support.microsoft.com/en-us/office/data-security-and-python-in-excel-33cc88a4-4a87-485e-9ff9-f35958278327.

To write to other cells, you would need to manually enter or do it via automation (i.e. an Office Script to write out the =PY formula) to the desired cell.

@jflam
Copy link

jflam commented Sep 25, 2023

You can also spill additional values back into the grid as well if you return lists, tuples or Pandas DataFrames from the Python formula.

@WillemWannenburg
Copy link

ahagmann83, have you found a work-around way to write from Python to Excel?

@jflam
Copy link

jflam commented Oct 23, 2023

ahagmann83, have you found a work-around way to write from Python to Excel?

There are no workarounds for this, as this behavior is by design. What is your scenario?

@WillemWannenburg
Copy link

I regularly build physics models on excel that calculate a certain output value based on given parameters. Then I always want to plot the relationship that a chosen input parameter has to the output. If I worked only in Python, it's easy: I would make a function and iterate (loop) to plot. Now, I want to achieve the same but using python in excel.
See the example below. Here I want to plot input parameter (C2) against output (C35). I achieved the desired output in "sheet=DP-Python", but this is not elegant enough, I would then rather just do it all in python.
I tried using the fact that python in excel goes left to right and then down, but then I get a circular ref issue.

Pressure analysis - python for excel.xlsx

If there is an elegant way to do this, it would work well for monte carlo simulations as well.
I know I can do this using a macro, but again then I'll just rather do it all in Python.

@salabim
Copy link

salabim commented Oct 24, 2023

I would think that you can dump several output variables as a dataframe on your sheet. From there you could possibly reference to other cells on your sheet(s).
If you want, we could discuss the details in private.

@WillemWannenburg
Copy link

In summary what I would want to do in a python cell in excel:

for i in [0,5,10,15,20,25,30,35,40,45,50]:
    xl("C2") = i  #write to excel
    output_list.append(xl("C35")) 
output_list

@WillemWannenburg
Copy link

I found out I can achieve my goal using a What-if -> Data Table. https://www.reddit.com/r/excel/comments/5sdlng/whatif_analysis_question/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants