LearnRevitAPI

Apr 23, 2023

Revit to Excel

While there are many plugins and scripts available out there to export and import data from Revit. it’s still worth knowing how to export your data with python so you can customize every step of the way for your own workflow.

👇 So let's explore how to Read and Write excel files in our pyRevit scripts.

1️⃣ xlsxwriter & xlrd

There are many different packages available to work with Excel in python. But I am going to show how to use xlsxwriter and xlrd.

The reason is simple - it's included in pyRevit by default!

💡 You can see all available packages in pyRevit in site-packages folder in GitHub Repository

1️⃣ Prepare Your Data

Before we Read/Write Excel Files, we need to understand how to prepare our data.

We need to organize our data in a list of lists, where each list represents a row and each value inside that list represents a value for corresponding column.

👇 Have a look on the data in excel and how it should be prepared in python.

So when you collect your data think about how you should iterate through your elements to organize your data right.

Below you can find an example on getting room parameters and creating a list of lists to export to Excel. There are a few steps

- Get Parameter Names + add ElementId
- Create a list of parameter values for a row 
- Get ElementId (To match data to the right element later)
- Get Parameter Values (In same order as parameter names)
- Append row to data

2️⃣ Filepath

Once your data is ready we can start creating a filepath.

You will see 3 steps in the snippet below to make sure that we have a unique filename and all necessary directories are created.

1. Create directory
First of all we need a folder for our Excel files. I like to use folder where python file is located for reports. Then I will append Excel to the path and create this folder if it doesn't exist yet.

2. Filename
I like to keep today's date in the filenames, I find it quite useful in long-term.

3. Unique Filepath
Lastly, I don't want to override existing excel files, so make sure that the filepath is unique. To keep it simple I usually add some symbol in the end until it's unique.

💡 Also I use for loops instead of while loops, because I got stuck in an infinite while loops in the past by forgetting break statement or never reaching it, so I default to for loops with large range of iterations instead.

3️⃣ Write Excel Files

So we have the data and the filepath, and we are ready to create and write in an Excel File. It's quite easy and you can use the Snippet below for that. 

1. Create Workbook (Excel File)
2. Create Worksheet
3. Then Write Data

💡You will notice that I used enumerate method for data and row_data.
Enumerate will add counts of your data in list, and we need to count it to write data to the right row and column number in Excel, which matches the structure in our data list.

4️⃣ Read Excel Files

If you want to read Excel files we will use xlrd package.

First of all we want to get all the data, and then we can read ElementId to get the right Element if you want to update some values.

💡Be aware that if you want to update values in Revit you need to make sure that parameters are not ReadOnly and nothing obstructs to override the values. 

For example some parameters of elements in the group do not vary across groups and therefore we will have hard time overriding them.

This snippet will get you your data from Excel and you can use it as you see fit.

5️⃣ How to update Revit Values?

If you want to update revit values from the Excel, you need to keep in mind to what element does the data correspond to and if it is possible to override it.

Usually you can use ElementId to easily get the right element, and then test if values you are trying to update are not ReadOnly, and parameter varies across groups or element is ungrouped.

I will leave this step to you, my goal was to show you how to Write and Read data from Excel, and then you can manipulate however you like.

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 6700+ people!