Import data from excel type xlsx

Import data from excel type xlsx

barbaracc6
Participant Participant
4,194 Views
9 Replies
Message 1 of 10

Import data from excel type xlsx

barbaracc6
Participant
Participant

Hello everyone, once more!

Right now i have a question more theorical. 
So, i was trying to make my python code more dynamic and import some data from an excel and then export another data to that same excel. The thing is the excel file type i want to work with is xlsx. I have not found a way to do this by working with Fusion API and VSCode associated. 
For example, i know there are a few libraries that can do that science data analysis and connection with python (for example, pandas). However, even though having that resource on VSCode, the tool cannot be used. 

I want to know if there is a way to do this in Fusion API or if there is a way to able python libraries in Fusion API.

I want to thank any help to this question. It would really help me on my project 🙂

0 Likes
Accepted solutions (3)
4,195 Views
9 Replies
Replies (9)
Message 2 of 10

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

First of all, this post has valuable information on how to import some python modules.

 

I also made some tests, and it worked for me in the following way:

- The python version Fusion360 uses is 3.9 as for now. You can check it from a script with the following code inside a script:

import sys

_app = adsk.core.Application.get()
_app.log(f'{sys.version_info[0]}.{sys.version_info[1]}')
 
I got: "3.9"
 
- Since I have regular python installation newer (3.10.1) than Fusion's, I decided to install numpy and pandas packages with the Fusion's Python, which is located in the following path:
C:\Users\<user>\AppData\Local\Autodesk\webdeploy\production\3dd5f1c145923035964be467e698157c79d9caeb\Python\python.exe
The following are the steps:
1. Use anaconda to create a new package environment (and make sure to run Fusion with this environment active).
2. Open command prompt and run fusion's python (with the previous path) to install modules :
pyhton -m pip install numpy
python -m pip install pandas
These will install the modules numpy and pandas with the right version on the environment you just created.
 
Now from the script in Fusion360 you can load the xlsx file with the following code:
import pandas as pd
dfs = pd.read_excel("c:\\tmp\\data.xlsx", sheet_name=None)  # use full path for file location
_app_log(f'{dfs}')  # this will print the content of the xlsx file; dfs is a pandas's dataframe with spreadsheet info.
 
You have to take into account that when autodesk updates Python version on a future release, an upgrade of the modules will be needed, uninstalling (python -m pip uninstall <module>) and installing them back.
 
Hope this help!!
Message 3 of 10

BrianEkins
Mentor
Mentor
Accepted solution

I would recommend taking a look at pylightxl. It's a very light Python library that supports reading and writing XLS files. It doesn't have any dependency on other libraries other than standard Python libraries so I was able to copy it to a sub folder of my add-in and reference it from there. This is much better than trying to install it in the Python sys folder or messing with the sys path. It also makes it practical to deliver your add-in to other computers because all you need to do is deliver this additional folder in your add-in folder.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 4 of 10

barbaracc6
Participant
Participant

Hello, thank you very much for all the help provided @Jorge_Jaramillo,.

Unfortunately, I still didn't get it to work. Once I have never worked with Anaconda environments, I found some difficulties following the steps you posted. I'm pretty sure I am doing something wrong.

As you can see in the following picture, I have created an environment in Anaconda named Fusion360 and I did manage to install the packages I wanted using Anaconda command prompt. 

Anaconda Environment packagesAnaconda Environment packages

 

I have also verified the version and got 3.9. (actually 3.9.6)

I have copied and pasted the path from fusion's python version, as you explained, and tried installing the libraries following that command.

 

Anaconda promptAnaconda promptcmd error installing packagescmd error installing packages

 

As you can see, I'm getting some sort of syntax error and I really have no idea how to fix it. I also don't know if there is any incompatibility caused by anaconda's python version on the interface being 3.9.12.

 

I have also tried doing the same with windows cmd and got the same issue.

Do you have any idea of what could be causing this error or am I way off with what I'm trying to do?

 

I am sorry to bother you again with this and I'm sure you're trying to be as simple as possible with the explanations, but I really have no experience with theses kinds of installations...

 

Thank you again for all the help.

0 Likes
Message 5 of 10

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi @barbaracc6 ,

I agree with the solution made by @BrianEkins on this thread.

But if you still want to use pandas (or any other python module), I made some more tests and this is result without using anaconda which is harder to implement.

 

This is the step by step:

1. From command prompt (I'm using windows here):

mkdir \fusion

cd \fusion

pip install virtualenv

 

2. From Fusion 360 create a new script (testPandas in my case) with the following code and run it:

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        import sys
        app.log(f'sys version_info: {sys.version_info}')
        app.log(f'sys executable: {sys.executable}')
        app.log(f'sys path: {len(sys.path)}')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

This is to check Fusion's python executable path.  In the Text Command window in Fusion you will get something like this:

Captura de pantalla 2022-06-21 090040.png

From here copy the path in the second line:

C:/Users/<username>/AppData/Local/Autodesk/webdeploy/production/59c8a288d74003935db5128cd3acb5349f9d794a/Python\python

 

3. Back in the command prompt on step 1 (a single line, change in above path slashes by backslashes, and set <username> accordingly):

 

virtualenv -p  C:\Users\<username>\AppData\Local\Autodesk\webdeploy\production\59c8a288d74003935db5128cd3acb5349f9d794a\Python\python.exe py39_fusion 

 

This will create a virtual python environment under the py39_fusion directory:

 

Captura de pantalla 2022-06-21 092140.png

 

 

4. Activate the new created virtual environment with the command:

py39_fusion\Scripts\activate

 

Captura de pantalla 2022-06-21 092649.png

 

You will notice the command prompt has changed from "C:fusion>" to "(py39_fusion) C:\fusion>"

Also if you check python version with the command "python --version" that you are using the same as it was reported by Fusion on first line on step #2.

 

5. Install numpy and pandas modules, from command prompt:

pip install numpy

pip install pandas

pip install openpyxl

 

Captura de pantalla 2022-06-21 104755.png

You can check the content of the py39_fusion\Lib\site-packages directory for the new modules installed:

 

Captura de pantalla 2022-06-21 105125.png

At this point the virtual environment is set.  We just need to instruct python on Fusion to use it to load pandas module from here. Copy this path: C:\fusion\py39_fusion\Lib\site-packages as you will need in the following step.

 

6. Back to the fusion script, change the script with the following code:

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        _PATH = 'C:\\fusion\\py39_fusion\\Lib\\site-packages'
        import sys
        app.log(f'sys version_info: {sys.version_info}')  # check Fusion's python version
        app.log(f'sys executable: {sys.executable}')      # get Fusion's python path
        if not _PATH in sys.path:
            sys.path.append(_PATH)                        # add module's path to sys.path if it doesn't
            pass
        app.log(f'sys path: {sys.path[-1]}')              # print last path in sys.path
        app.log(f'sys path: {len(sys.path)}')             # get sys.path's length

        import pandas as pd
        dfs = pd.read_excel("c:\\fusion\\data.xlsx", sheet_name=None)
        app.log(f'{dfs}')
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

7. Finally run the script and check for the data in Text Commands window:

 

Captura de pantalla 2022-06-21 105827.png

 

You need to repeat steps #4 and #5 if you need to add other python module; and steps #2 to #5 in case Fusion360 is updated with a new python version.

This way the new modules are being installed in a independent location other than Fusion's location, without being intrusive to it; and just adding the reference to the virtual environment, will allow to use other python modules. Restarting Fusion without running the script will clear/reset the environment.

 

Hope that this step by step is clearly enough this time ;-).

 

Best regards,

Jorge

 

0 Likes
Message 6 of 10

barbaracc6
Participant
Participant

Thank you very much for all the help provided. 
I managed to import and use python libraries as i asked for. 
Thank you very much @Jorge_Jaramillo and @BrianEkins for all the help!

0 Likes
Message 7 of 10

urel.vitorR2KNY
Explorer
Explorer

Hello Jorge,

 

I have the same issue importing numpy and pandas libraries.
I followed your step by step, but I got an error in step 3 that I couldn't solve.

When writing the command "virtualenv -p \Users\<UserName>\Library\Application Support\Autodesk\webdeploy\production\2fabb790fc4b4ae8fc5db8bee740ff206855e492\Autodesk Fusion 360.app\Contents\Frameworks\Python.framework\Versions\Current\bin\python.exe py39_fusion " in the terminal, the following error appears: RuntimeError: failed to find interpreter for Builtin discover of python_spec='Users<UserName>LibraryApplication'

 

Could you help me fix it and import the libraries?

 

Thank you in advance for your attention.

Best, 

Vitor Urel

0 Likes
Message 8 of 10

Jorge_Jaramillo
Collaborator
Collaborator

Hi @urel.vitorR2KNY ,

 

You forget to replace <UserName> in the path as instructed in the step #3 (note in parenthesis):

 

. . .

When writing the command "virtualenv -p \Users\<UserName>\Library\Application Support\Autodesk\webdeploy\production\2fabb790fc4b4ae8fc5db8bee740ff206855e492\Autodesk Fusion 360.app\Contents\Frameworks\Python.framework\Versions\Current\bin\python.exe py39_fusion " in the terminal

. . .


Are you using Mac or Win?

What are you getting from step #2 for sys.executable?

 

The path I have with the current Fusion360 version is:

C:\Users\<UserName>\AppData\Local\Autodesk\webdeploy\production\8814bf1cdd9624465cbdf7f023becc629a5ffbe5\Python\python.exe

 

You can check if the correct with the help of the file explorer, following each name in the path, until you reach python.exe at the end.

 

Regards,

Jorge

 

0 Likes
Message 9 of 10

urel.vitorR2KNY
Explorer
Explorer

Hello Jorge.

 

Actually I put my User Name. (I left it here without it just to be safe),
but I rode it just right all the path.

I'm using a mac.

 

Here's an image of what I get from the function sys.executable

 

Thanks for the answer.
If you know anything I can do to get it installed, it would be great.

Captura de Tela 2022-08-24 às 15.41.25.png

0 Likes
Message 10 of 10

Jorge_Jaramillo
Collaborator
Collaborator

Hi @urel.vitorR2KNY ,

 

Could you try to append "/python.exe" at the end of that path, and run it with "--version" option from command prompt?

Like so:

wtallerdemadera_0-1661375362425.png

What version of virtualenv are you using? (you can git with "virtualenv --version" from command prompt).

 

I believe you have to replace "\" by "/" since you are on Mac.  Could you give it a try?

 

Regards,
Jorge

 

0 Likes