Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xl() function key error
#1
So the Microsoft Excel preview version includes python integration. I am having trouble with it and I posted on the MS office preview forum but the Microsoft moderator marked my post out of scope and suggested I post here instead so I figured I'd give it a try.

In the Excel version of python you can use the xl() function to turn an Excel range into a Python DataFrame (as explained here: https://support.microsoft.com/en-us/offi...B1%3AC4%22)

So, for example, when I hardcode the range it works fine like this:
 df = xl("A1:C3") 
Output:
0 1 2 0 A None None 1 None B None 2 None None C
But when I use a string variable instead of a hardcoded string I get a key error:

str1 = "A1:C3"
df2 = xl(str1)
Error:
Error:Python KeyError: 'A1:C3'
Strangely, if I create a hardcoded version in the same Excel cell script then the string version works, but only if the hardcoded version is exactly the same as the string version which isn't very helpful.
df3 = xl("A1:C3")
str2 = "A1:C3"
df4 = xl(str2)
print(df4)
Output:
0 1 2 0 A None None 1 None B None 2 None None C
Does anyone here have any guesses about what is happening? I want to dynamically create DataFrames from excel ranges but it seems to only want to work when I hardcode it which limits the functionality a lot.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error in using the output of one function in another function (beginner) MadsPJ 6 6,303 Mar-13-2017, 03:06 PM
Last Post: MadsPJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020