Open Excel File with Python


#1

Hi All,

Quick question. How do I open a particular Excel file using python? Don’t want to write or read the info, just open the file?

Thanks in advance!


#2

Why exactly do you want to use Python code for that?

You can simply use the Excel.ReadFromFile node to read data from an Excel file and an Excel file will always open to let Dynamo do so.

A script like the following should do the job:


#3

Hi @PauLtus, I’d like to use it first, to know how can I call excel files from python and also because it is a part of a larger python code.

The example you mentioned it is what I want but using only python. Is it possible?

Thanks in advance!


#4

#5

Well, you probably can.

But in that case maybe you can just load in the file in a similar sense to as you’d do with other inputs:


Following that, I’m an absolute Python amateur.
Might be a case where you’d just want to ask it on a Python forum.


#6

Thanks @PauLtus


#7

It’s possible! If you want to background-open the Excel file for editing (as part of your Python script) you can double-click on Konrad’s Write Excel node in the Bumblebee package to read through the process.

I have no idea about your level of experience - it’s not for beginners. Also, check out the Microsoft Office Interop library for Excel. If this process doesn’t make sense to you, I would just suggest using Dynamo.


#8

Thanks @oliver.green. I managed to do this (the path to the excel file was stored in my project information):

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import Autodesk

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

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

doc = DocumentManager.Instance.CurrentDBDocument
ruta = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_ProjectInformation).ToElements()[0].LookupParameter('RUTA PLANILLA MUROS').AsString()

#abrir el libro de excel y extrae las columans relevantes
ex = Excel.ApplicationClass()
ex.Visible = True

workbook = ex.Workbooks.Open(ruta)

OUT = 'Listo!' 

Cheers!