Accessing XLSX files from pyrevit

Hello,
I have some DB in an xlsx file, that I need to access from my macro on pyrevit.
For that i imported openpyxl module (which is already installed from PIP).

But i have the following message error on shell console:
ImportError: No module named openpyxl

here after my program:

#! python 3
“”“Application des Categories”""

-- coding: utf-8 --

title = “Categorie”
author = ‘Anes Chakroun’

uidoc = revit.ActiveUIDocument
doc = revit.ActiveUIDocument.Document
docs = revit.Application.Documents

import clr
clr.AddReference(‘System.Windows.Forms’)
from System.Windows.Forms import OpenFileDialog

dialog = OpenFileDialog()
dialog.Filter = “Fichier xlsx|*.xlsx”
if dialog.ShowDialog():
selectedFile = dialog.FileName

print(selectedFile)

import openpyxl
wb_obj=openpyxl.load_workbook(selectedFile)

sheet=wb_obj.active

print(sheet[“D7”].value)

I do not think openpyxl is included with embedded cpython.

An alternative to deal with excel files in ironpython is to use:

import clr
clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
from Microsoft.Office.Interop import Excel

Konrad Sobon and David Mans built this library that I use:

2 Likes

Thank you Steve for your prompt reply.
What I can say that the method you propose need that Excel or Office is already installed on windows. So it’s dependent in someway to the softwares on each PC.
I have seen that in one forum that Ehsan Iran-Nejad has already implemented on the packaged CPython: xlrd and xlsxwriter modules. The first is outdated now and don’t give access to the new file format of excel. The second is nice and rich but can only create new xlsx file but can’t modify existing one’s.

openpyxl seem to be capable to do all that in one unique module: read, modify, create, write. And seem to be enough active.

So a general question, is it possible to import 3rd party modules non already packaged in CPython of pyrevit?
If it’s possible? how it can be done?

Finally, I’ve succeeded to make possible the use of third party python modules.
Thanks to this thread.

https://forums.autodesk.com/t5/revit-api-forum/how-to-use-python-3-libraries-through-pyrevit/td-p/10173809

I will try to explain the procedure for who need it:

  • In fact, since pyrevit 4.7 version, it’s possible to use CPython instead of IronPython, letting us to use more common modules that are not compatible with IronPython. To do that, we have to force the script to use CPython by adding this line:

#! python3

at the first line of the script.

  • More over, we have to add on windows system variable environment, the key PYTHONPATH pointing to the site-packages folder of your CPython version installed. This key can be added through advanced parameter manager:


    A restart ow windows should be done to make this setting effective.

  • Be aware that the imported module is compatible with the version installed in the package of pyRevit and have the same revision. for example 3.8.* or 3.7.*

That’s all. Hope it helps.

5 Likes