Read and add in a dictionary values from EXCEL - SOLVED

Hello,
I actually have a python code to read and retrieve data values into a dictionary using import CSV and DictReader.
Simple datas well organized of customers (name, email, phone…)
It works great, but I would like to know if reading data from EXCEL file could be done easily? (to avoid export in csv the excel base file)
Which import need I use?
I would like to have a cross system between MAC and PC

Thank you for your helps

If I understand your question correctly, you’re asking how to read an Excel file using Python. With the addition of the CPython component(python 3 script component) to Rhino 8, you can install libraries to accomplish this task(pip install ...). There are numerous examples available online. For instance, I’ve previously created a basic solution, as shown in the image below:
using openpyxl library…

thank you Sadra, I’ll test this library, you perfectly understand my question, I was only looking for a compatible with R8 of a library for reading excel.

Hi Antione,

I’m not sure of your exact needs, but often I use the components inside Lunchbox to import the data directly into Grasshopper and then plug that data into a second node that contains my scripts. Lunchbox can be found at:

Ash

@Ashley_Cichocki thank you but, I am not working inside Grasshopper, i was needing a library to import excel directly from a py program.:slight_smile:

.NET comes with Excel interop (i.e. that one can implement in IronPython), that’ll probably cover all your needs. Here’s a quick implementation example in GHPython, but the same code should work in IronPython in Rhino 8:


210526_GHPythonExcelInterop_00 (1).gh (1.8 KB)

2 Likes

Hello @sadra,
I installed openpyxl, then from rhino script editor, only create a py file to import and print openpyxl:
It seems that the library is well found in rhino directory, and ready to be import, but when I use import pyxl on my py file, I have an error, module openpyxl not found…
something wrong in the install?
Thanks for your helps

Traceback (most recent call last):
  File "file:///C:/Users/Antoine/.rhinocode/stage/dd0jpswn.qug", line 1, in <module>
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\__init__.py", line 7, in <module>
    from openpyxl.workbook import Workbook
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\workbook\__init__.py", line 4, in <module>
    from .workbook import Workbook
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\workbook\workbook.py", line 7, in <module>
    from openpyxl.worksheet.worksheet import Worksheet
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\worksheet\worksheet.py", line 25, in <module>
    from openpyxl.cell import Cell, MergedCell
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\cell\__init__.py", line 3, in <module>
    from .cell import Cell, WriteOnlyCell, MergedCell
  Fi
le "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\cell\cell.py", line 26, in <module>
    from openpyxl.styles import numbers, is_date_format
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\styles\__init__.py", line 4, in <module>
    from .alignment import Alignment
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\styles\alignment.py", line 5, in <module>
    from openpyxl.descriptors import Bool, MinMax, Min, Alias, NoneSet
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\__init__.py", line 4, in <module>
    from .sequence import Sequence
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\descriptors\sequence.py", line 4, in <module>
    from openpyxl.xml.functions import Element
  File "C:\Users\Antoine\AppData\Local\Programs\Python\Python312\Lib\site-packages\openpyxl\xml\functions.py", 
line 28, in <module>
    from xml.etree.ElementTree import (
ModuleNotFoundError: No module named 'xml.etree'

Excel files are just xml under the hood. If you’re working in IronPython and want something simpler than the .Net interop, in pure Python, and can manage adding the path to sys.path to import a library, then I’ve tested many of the libraries in this list: https://www.python-excel.org/

I would’ve used openpyxl as well, but I could not easily get it to work. The simplest way to create a working Excel library I could find was a one character change to the actual code, to create a fork of pylightxl:

Minimise your dependencies and try the parent project, pylightxl if you’re using Rhino 8, CPython components.

1 Like

@James_Parrott Thank you for your help, but I have a problem installing from pip, they had been installed into my rhino folder rh3,9-rh8\site-packages but any time i try to import in my py file, at launch it tells me that "no module named pylightxl "(or openpyxl)… any idea?
Wrong install folder?

That error is exactly what happens when Python can’t find the library, so it’s almost certainly installed incorrectly (or the wrong path is on sys.path, if you’re using the old fashioned way).

I havent tried it yet, but it’s easier to get CPython components to handle the install with a new comment syntax. Then you don’t need to use pip at all (pip does have a custom install directory option though, but it’s an obvious unnecessary source of problems, installing packages with one Python version’s pip, to be imported by another).

seems not working on Mac… couldn’t add reference assembly…

@James_Parrott Thank you for your helps but I have a still a problem, when lauching a simple py, to read my excel file and print datas, from the script editor, no problem with loading pylightxl, or openpyxl, but when using this py file as a button, the loading of library does not function, no modules…
As if lauching from editor or from rhino are not loading libraries the same way… I even add in the beginning of my code # r:pylightxl but it is like if from rhino button, r: is not read to load the external library…
Could you reproduce my problem… or not?

I can’t at the moment, I’m afraid. Perhaps someone else will try to reproduce it if you provide a short example script.

But basically it sounds like the context is different in the script editor, to when executing as a Button.

I actually find Rhino plug-ins are a lot trickier than Grasshopper ones (admittedly I’ve spent an order of magnitude more time on the latter). Depending on what you want to do with your code, and depending how your user will install it, the environment and context your code will be running in can change. Once that context is decided, you can work out construct a path for the libraries your code needs, e.g. in a subfolder of the regular installation folder.

If your code is just for you, it’s straightforward to use the shared working directory ('.'), or user’s libraries folder. Otherwise it’s possible to use the API to find the path of the Rhino document or Grasshopper document, and the paths of the special Grasshopper folders plug-ins often require installation into (libraries and user objects).

Hi, I apologize for the delayed response. I utilize Python libraries within Grasshopper using the Python 3 component. I haven’t tested it in the Rhino Python editor, so there might be some version-dependent bugs. Have you tried it in Grasshopper with the Python 3 component?

as you can see,there is a problem with rhino/python

Hello,

Just a following post on this subject to explain that I solved it , thank to @eirannejad and @nathanletwory .
I actually use Openpyxl library, but same process could be done using Pylightxl.
The trick is to load library on top of your program using # r:openpyxl
Pleased find the py files which is able from a simple Eto formulary to access to a Xlsx file put in your computer to manage datas, and retrieve them in the EtoDialog and be able to populate you user texts with datas from excel.
Inside the py file, need to adjust a picture link, and create an Excel file as pictures with Worksheet and matching columns, then populate it, and it should works.
PROJETFORM.py (13.3 KB)
Hope this could help people… Enjoy

Anyway still a bug when loading from a Mac directly from a button -runrhinoscript…because openpyxl is not loaded… but it works from Scripeditor on Mac… PC no problem :slight_smile:
7b2a2a0d44934ec462b2d1404566d6b8f0dd3dfa_2_690x169
b4c5631cd5227f3f88d5775173a590c93cd7b9c8_2_690x161