Share via

Excel =PY() function

Anonymous
2024-01-07T18:39:13+00:00

Hello,
I started to learn python recently.
and I have 2 questions:

  1. when i tried basic code to create "myfile.txt" it worked fine in VS code, but then i copied this to excel I got error 13, permission error.
    So how do I write data frame into csv file from the =Py() function, especially since im not able to create basic txt file?
  2. I know how to "import" cells into python command.
    but say in A4 I have my python formula, the question I have is:
    typing python code in A4: how to I write any value to say C4, using that =PY() formula from A4?
Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2024-01-07T22:22:39+00:00

Hi Radoslaw,

Thanks for contacting us,

I can help you with your questions about the =PY() function in Excel.

  1. To create or write files from the =PY() function, you need to specify the full path of the file location, such as "C:\Users\YourName\Documents\myfile.txt" or "C:\Users\YourName\Documents\myfile.csv". Otherwise, you may get a permission error or a file not found error. For example, to write a data frame into a csv file, you can use the pandas library and the to_csv() method¹. Here is a sample code that you can use in the =PY() function:
import pandas as pd
df = pd. DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 32, 28]})
df.to\_csv("C:\\Users\\YourName\\Documents\\myfile.csv", index=False)
  1. To write a value to another cell from the =PY() function, you need to use the xlwings library and the Range() object. The Range() object allows you to access and modify Excel cells and ranges from Python. Here is a sample code that you can use in the =PY() function:
import xlwings as xw
xw. Range("C4").value = "Hello"

This will write the value "Hello" to cell C4 from the =PY() function in cell A4.

Let me know if this helps or if you need further assistance.

Regards, Sola

“Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below. “

Kindly note that this is a user to user forum, we are users helping other users, we aren't Microsoft employee neither are we Microsoft agents.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Anonymous
    2024-01-08T05:49:04+00:00

    Hello and thank you,

    I will try this.

    but please answer me this:
    def main():  

    Open a file for writing and create it if it doesn't existf = open("textfile.txt","w+")

    Why this code even when the full path is specified works in VS code, but doe not work with =PY() excel function?

    Was this answer helpful?

    0 comments No comments