Schedule Field Padding Before Export

New to Dynamo and the Python language as a whole; having to learn on my feet a bit. Have a schedule within Revit that want to export out to a text file. That part was easy with a little bit of Dynamo and code from this site. I am not though looking at padding one of the fields within the schedule so that when it is exported out to a text file it is 3 characters in length and has 0’s in the front. Any help would be appreciated. Below is my node breakdown and code.

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

schedule_list = IN[0]
path = IN[1]
filename_list = IN[2]
result_list = []
names = []

for index, sched in enumerate(schedule_list):
schedule = UnwrapElement(sched)

filename = filename_list[index]
try:
export_options = ViewScheduleExportOptions()

export_options.ColumnHeaders = ExportColumnHeaders.None
export_options.Title = False
export_options.FieldDelimiter= ";"
export_options.TextQualifier = ExportTextQualifier.None

definition = schedule.Definition
ids = definition.GetFieldOrder()

for id in ids:
param = definition.GetField(id)
names.append(param.GetName())

schedule.Export(path, filename, export_options)
result_list.append("Schedule exported ")
except: result_list.append("Export failed ")
OUT = result_list

I don’t think you can modify the data during the export. You would either have to modify the schedule before exporting or modify the text after exporting. PadLeft is how you would do that.

The zfill() method in Python achieves exactly this, however it requires modifying your data. Therefore, you can’t directly export the schedule as you are currently doing. You can use Bimorph nodes to get data from the schedule, as well as a String.PadLeft (if you want to do everything using nodes) as an alternative to zfill in python.

I am actually doing it before exporting at this point. Down and dirty way to pad it but it is working, now just need to export from there.

This is where the fun begins, I have the data changed to what I need it to be, however is there a way to insert it into the ViewTable to be exported from here?

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import*

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application


schedule_list = IN[0]
path = IN[1]
filename_list = IN[2]
result_list = []
names = []

for index, sched in enumerate(schedule_list):
	schedule = UnwrapElement(sched)
	filename = filename_list[index]
	
	TransactionManager.Instance.EnsureInTransaction(doc)
	
	table = schedule.GetTableData().GetSectionData(SectionType.Body)
	iRows = table.NumberOfRows
	iColumns = table.NumberOfColumns
	
	temp = []
	data = []
	i = 1
	
	for row in range(iRows):
		dataCol = []
		for column in range(iColumns):
			dataCol.Add( TableView.GetCellText(schedule, SectionType.Body, row, 0) )
			
			#Left Pad the Department Number Field with 0's if required. Field is set to an integer
		
			temp = TableView.GetCellText(schedule, SectionType.Body, row, 1)
			temp = str(temp)
			i = temp.Length
			
			if i == 2:
				temp = "0" + temp
			
			if i == 1:
				temp = "00" + temp
		
			#End of Padding
			
			#Setup Data Set for export.
			dataCol.Add(temp)
			dataCol.Add( TableView.GetCellText(schedule, SectionType.Body, row, 2) )
			dataCol.Add( TableView.GetCellText(schedule, SectionType.Body, row, 3) )
			dataCol.Add( TableView.GetCellText(schedule, SectionType.Body, row, 4) )
			dataCol.Add( TableView.GetCellText(schedule, SectionType.Body, row, 5) )
			dataCol.Add( TableView.GetCellText(schedule, SectionType.Body, row, 6) )
			
			#Add to final Data Set.
				
	TransactionManager.Instance.TransactionTaskDone()
	result_list.append(data);	
	
	try:
		export_options = ViewScheduleExportOptions()
		export_options.ColumnHeaders = ExportColumnHeaders.None
		export_options.Title = False
		export_options.FieldDelimiter= ";"
		export_options.TextQualifier = ExportTextQualifier.None
		schedule.Export(path, filename, export_options)
		
		#Success or Failure reporting.
		result_list.append("Schedule exported ")
	except: result_list.append("Export failed ")
	
OUT = result_list

The TableSectionData class (which is the data type of your table variable) has a SetCellText method, although this would most likely change the actual parameter value in your model. I would recommend getting the data yourself, storing it in a list of lists, modifying that data, and exporting it to a txt file using the csv module.