简体   繁体   中英

How do you take a function from an excel spreadsheet and run it with python?

Hello whoever is reading this. I am having trouble taking a python function from an excel spreadsheet and then running it in python. I'd like to stress on the fact that I actually defined my function in python, and it's not an excel function.

Please note that this is my second time questioning and have only ever done coding as a hobby.

Here is an example of what I want to take: heal(2,c)

I am using xlrd to analyze data on the spreadsheet. Here is a chunk from my code.

    e = worksheet.cell(rowidx,colidx+1)
    f = str(e).replace("'","")
    g = f.replace("text:","")

This chunk focuses on converting the 'cell object' to a 'string' and making it look like the function required. The end result is this:

    g = heal(2,c)

My problem is that I cannot seem to activate this function. I have also tried doing "g()" but it came up with the error message: File "C:\\Users\\Alexander\\Dropbox\\Documents\\Python\\Hearthstone\\Hearthstone.py", line 18, in play g() TypeError: 'str' object is not callable

I do not mind if you tell me a way to activate "g" or just directly run it from the spreadsheet. Please let me know if you need any more information. Thank you for you time.

You can use eval for same. Here, I am considering that funciton is defined in current file only.

Example:

eval('heal(2,c)')

If function is defined in other file ie "file",import it and call using:

Example: import file

eval('file.heal(2,c)')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM