Export data as .csv from Revit externally/internally using Python

Hi Community,

Is that possible to use Python to export element parameters/data from Revit into csv files either inside of Revit program or without opening the program? I would like to discuss both ways. Appreciated!

1 Like

@ycaifu666 ,

i managed first doing it in dynamo it is easy going

i python i come until collecting, BUT not the export…

it would be recommonadable to make some kind of interface to get and set CSV

# PythonCode
__title__   = "Export CSV"
__author__  = "Andreas Draxl"
__version__ = "1.0"
__doc__ = "Version = 1.1"
#Date    = "28.06.2023"

import os
from pyrevit import forms
from pyrevit import revit
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import TaskDialog

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System")
from System.Collections.Generic import List
from System.Windows.Controls import ComboBoxItem
import wpf

PATH_SCRIPT = os.path.dirname(__file__)

uidoc   = revit.uidoc
app     = revit.app
doc     = revit.doc

name = "Export CSV"
transaction = Transaction(doc, name)
transaction.Start()

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType()
walls = list(collector)

output = []

for wall in walls:
    length_param = wall.LookupParameter("Länge")
    if length_param and length_param.StorageType == StorageType.Double:
        length = length_param.AsDouble()
        length_mm = UnitUtils.ConvertFromInternalUnits(length, DisplayUnitType.DUT_MILLIMETERS)
        output.append(length_mm)

transaction.Commit()

# Export to CSV
if output:
    csv_file_path = os.path.join(PATH_SCRIPT, "output.csv")
    with open(csv_file_path, "w") as f:
        f.write("Length (mm)\n")
        for value in output:
            f.write(f"{value}\n")
    TaskDialog.Show("Export Successful", f"CSV file exported to: {csv_file_path}")
else:
    TaskDialog.Show("No Walls", "No walls found in the project.")

print(output)

i get this warning :frowning:

KR

Andreas

add this at the beginning of your file

# -*- coding: UTF-8 -*-

when using ascii characters in string you have to specify the encoding.

if you want to have a path setting UI, use the forms module

pick_folder()

note that you have two written functions for csv in the script module

load_csv and dump_csv

1 Like