I am relatively new to Dynamo and Python scripting, and I’m in the process of understanding how to automate the export of a specific group of schedules.
This Python script, aims to simplify the task of exporting selected schedules from a Revit project to PDF files. The script accomplishes this by:
- Identifying a list of schedule names for export.
- Defining export settings for the PDF format.
- Locating each schedule in the Revit project by name.
- Creating file paths for each exported PDF.
- Executing the schedule-to-PDF export process.
- Providing a list of file paths for the exported PDFs.
The objective of this script is to enhance project management and documentation within Revit by streamlining the schedule export process. I’m currently facing some issues with the script’s functionality, it should be identifying and exporting schedules based on a predefined list of schedule names, it is currently experiencing issues in correctly filtering the schedules and exporting the data.
Would appreciate any assistance or guidance in resolving them.
Python Script:
# Import the required Dynamo libraries
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Input parameters
scheduleNames = IN[0] # List of schedule names to export
outputDirectory = r'C:\Users\Stephen.Forte\OneDrive - MJM Marine LTD\Desktop\RevitDynamoSchedulesExport(Test)'
# Get the current Revit document
doc = DocumentManager.Instance.CurrentDBDocument
# Print the list of schedules you're importing for export
print("Schedules to Export:")
for name in scheduleNames:
print(name)
# Create a list to store the file paths of the exported schedules
exportedFiles = []
# Start a transaction
TransactionManager.Instance.EnsureInTransaction(doc)
# Iterate through the list of schedule names
for scheduleName in scheduleNames:
try:
# Find the schedule view by name
collector = FilteredElementCollector(doc)
schedules = collector.OfClass(ViewSchedule).ToElements()
for schedule in schedules:
if schedule.Name == scheduleName:
# Export the schedule to a CSV file
options = ViewScheduleExportOptions()
options.FieldDelimiter = ","
options.Title = scheduleName
options.ExportScope = ExportScope.Schedule
options.ShowTitle = True
options.FilterByVisibility = False
# Build the full file path with the specified output directory
filePath = outputDirectory + "\\" + scheduleName + ".csv"
schedule.Export(filePath, options)
# Add the exported file path to the list
exportedFiles.append(filePath)
# Print a message to indicate successful export
print(f"Exported {scheduleName} to {filePath}")
except Exception as e:
# Log any errors that occur during export
exportedFiles.append(f"Error exporting {scheduleName}: {str(e)}")
# Print an error message
print(f"Error exporting {scheduleName}: {str(e)}")
# Commit the transaction
TransactionManager.Instance.TransactionTaskDone()
# Output the list of exported file paths
OUT = exportedFiles