# Need to export this script result to excel

**URL:** <https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767>\
**Category:** Revit\
**Created:** [May 22, 2022, 2:23am UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767 "2022-05-22T02:23:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![suhailmuhammedk01](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@suhailmuhammedk01](https://forum.dynamobim.com/u/suhailmuhammedk01)\
**Post date:** [May 22, 2022, 2:23am UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/1 "2022-05-22T02:23:57Z")

</div>

![Duct Schedule Level Wise_2022-05-22_05-21-05](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/2/e2fb326c8efbe575f77d1a39bd3faced936a44ea.png)

---

<div class="post-metadata">

**Author:** ![Elie.Trad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/elie.trad/32/110373_2.png) [@Elie.Trad](https://forum.dynamobim.com/u/Elie.Trad)\
**Post date:** [May 22, 2022, 12:17pm UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/2 "2022-05-22T12:17:28Z")

</div>

Hi @suhailmuhammedk01 and welcome, zoom in Dynamo so it is clear for you the names of the nodes then export an image of the graph, and try to post the error messages of the last nodes.

---

<div class="post-metadata">

**Author:** ![suhailmuhammedk01](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@suhailmuhammedk01](https://forum.dynamobim.com/u/suhailmuhammedk01)\
**Post date:** [May 23, 2022, 12:54am UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/3 "2022-05-23T00:54:32Z")

</div>

Warning: Data.ExportExcel operation failed.

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [May 23, 2022, 12:56am UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/4 "2022-05-23T00:56:37Z")

</div>

Probably the usual issue we face with Excel, which means you need to do an online repair.

---

<div class="post-metadata">

**Author:** ![suhailmuhammedk01](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@suhailmuhammedk01](https://forum.dynamobim.com/u/suhailmuhammedk01)\
**Post date:** [May 23, 2022, 1:02am UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/5 "2022-05-23T01:02:21Z")

</div>

> **[Duct Schedule Level Wise.dyn and 2 more files](https://wetransfer.com/downloads/043fb2e8877685a7392e1c21a924fe7520220523010146/f9eb9e)**
>
> 3 files sent via WeTransfer, the simplest way to send your files around the world

attaching here all files

---

<div class="post-metadata">

**Author:** ![suhailmuhammedk01](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@suhailmuhammedk01](https://forum.dynamobim.com/u/suhailmuhammedk01)\
**Post date:** [May 24, 2022, 4:35pm UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/6 "2022-05-24T16:35:30Z")

</div>

anyone please help dynamo forum

---

<div class="post-metadata">

**Author:** ![sovitek](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/sovitek/32/116237_2.png) [@sovitek](https://forum.dynamobim.com/u/sovitek)\
**Post date:** [May 24, 2022, 4:42pm UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/7 "2022-05-24T16:42:14Z")

</div>

Hi I would do as Gavin mentation but if dont want that then try this one here from archilab…it had worked for me, before we fix with the repair

![Screenshot 2022-05-24 184144](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/d/bd12be3bbaa2e7c4b977e17add5157d2cba3680f.jpeg)

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [May 24, 2022, 11:40pm UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/8 "2022-05-24T23:40:47Z")

</div>

FYI when you type `@Dynamo` it sends a notification to a user, not the full forum. There is no way to ping the entire forum either, it’d be over reaching if there were.

In this case you have a few things to test.

1. Does the path you are writing to exist? If not you may not be able to create the path at that location.
2. Can you write to a file there manually? If not you may have a permission issue, check with your IT team.
3. Does writing to excel at all work with a basic list of 1…10? If not you likely need to run the Online Repair tool which Gavin mentioned above. More info here: [Repair an Office application - Microsoft Support](https://support.microsoft.com/en-us/office/repair-an-office-application-7821d4b6-7c1d-4205-aa0e-a6b40c5bb88b)
4. Does the write out only fail with your code? If so you likely have a larger issue with your data structure. Check that closely.

As an alternative you could write to a CSV, or use a package that makes use of an XML based Excel writer, or another tool.

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [May 25, 2022, 2:02am UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/9 "2022-05-25T02:02:28Z")

</div>

I’m in the process of working with microsoft interop tools and have got Import Excel working (see below), but am still working through export Excel. Hit some road blocks but began a thread to work through them here:

> [@Export data to Excel using Python/Microsoft Interop](https://forum.dynamobim.com/t/export-data-to-excel-using-python-microsoft-interop/76899):
>
> I’ve been trying to pick apart the methods and logic used to export a basic 2D matrix of data to a worksheet in an existing file today. I managed to work with some code from Bumblebee to get importing to work consistently, but have been hitting a lot of errors with exporting of data. I know part of this relates to how COM objects are released and the XlApp is quit, as files are sometimes left in a readonly state until I restart, but generally the warnings indicate I’m also getting something wro…

I’ve come to the conclusion the online repair method just isn’t feasile, nor is upgrading to Revit 2022 right now and in some cases nor is getting Bumblebee and its Python libraries onto all machines in large firms. My solution is to try to make basic import/export nodes (single sheet/2D matrix) in local Python using the interop library by Microsoft. Yes security exploits, IronPython etc. but it’s really what I need and others probably will too.

Excel Import Python code (coming to Crumple soon-ish):

```auto
# Provided by Gavin Crump
# Free for use
# BIM Guru, www.bimguru.com.au

# Code borrowed and adjusted from Bumblebee
# Free for use under lisence on github
# https://github.com/ksobon/Bumblebee

# Boilerplate text
import clr
import System

# import excel for reading
clr.AddReference("Microsoft.Office.Interop.Excel")
from Microsoft.Office.Interop import Excel
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo("en-US")
from System.Runtime.InteropServices import Marshal

# sets up Excel to run not in live mode
def SetUp(xlApp):
	# supress updates and warning pop ups
	xlApp.Visible = False
	xlApp.DisplayAlerts = False
	xlApp.ScreenUpdating = False
	return xlApp

# read excel data of a worksheet
def ReadData(ws, origin, extent, byColumn):

	rng = ws.Range[origin, extent].Value2
	if not byColumn:
		dataOut = [[] for i in range(rng.GetUpperBound(0))]
		for i in range(rng.GetLowerBound(0)-1, rng.GetUpperBound(0), 1):
			for j in range(rng.GetLowerBound(1)-1, rng.GetUpperBound(1), 1):
				dataOut[i].append(rng[i,j])
		return dataOut
	else:
		dataOut = [[] for i in range(rng.GetUpperBound(1))]
		for i in range(rng.GetLowerBound(1)-1, rng.GetUpperBound(1), 1):
			for j in range(rng.GetLowerBound(0)-1, rng.GetUpperBound(0), 1):
				dataOut[i].append(rng[j,i])
		return dataOut

# exit excel once worksheet is read
def ExitExcel(xlApp, wb, ws):
	# clean up before exiting excel, if any COM object remains
	# unreleased then excel crashes on open following time
	def CleanUp(_list):
		if isinstance(_list, list):
			for i in _list:
				Marshal.ReleaseComObject(i)
		else:
			Marshal.ReleaseComObject(_list)
		return None
		
	xlApp.ActiveWorkbook.Close(False)
	xlApp.ScreenUpdating = True
	CleanUp([ws,wb,xlApp])
	return None

# inputs
filePath = IN[0]
sheetName = IN[1]
byColumn = IN[2]
report = []

# try to get Excel data
try:
	xlApp = SetUp(Excel.ApplicationClass())
	xlApp.Workbooks.open(unicode(filePath))
	wb = xlApp.ActiveWorkbook
	ws = xlApp.Sheets(sheetName)
	originGot = ws.Cells(ws.UsedRange.Row, ws.UsedRange.Column)
	extentGot = ws.Cells(ws.UsedRange.Rows(ws.UsedRange.Rows.Count).Row, ws.UsedRange.Columns(ws.UsedRange.Columns.Count).Column)
	dataOut = ReadData(ws, originGot, extentGot, byColumn)
	ExitExcel(xlApp, wb, ws)
	report = "Data retrieved successfully."
except:
	dataOut = None
	report = "Data not retrieved successfully. Make sure sheet name is correct"

# return result
OUT = [dataOut,report]

```

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [May 25, 2022, 6:47am UTC](https://forum.dynamobim.com/t/need-to-export-this-script-result-to-excel/76767/10 "2022-05-25T06:47:24Z")

</div>

Just bumping this back up to confirm @c.poupin solved this one for me by sharing a python library they made that I adjusted to suit a custom node format. The code is available over on the previously mentioned thread and I’ve got it to work on computers with repair issues.
