Excel Fill Color

All,

Thank you for your help and comments. A Coworker and I were able to get what we needed. @c.poupin your code was extremely helpful.

The modification is not beautiful but get the job done for now and what we need. I will hopefully clean up the script in the near future.

#https://forum.dynamobim.com/t/excel-fill-color/46984/5
# coding: utf-8 
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

import System
from System import Array
from System.Collections.Generic import *

clr.AddReference('System.Drawing')
import System.Drawing
from System.Drawing import *


clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' )
from Microsoft.Office.Interop import Excel
from System.Runtime.InteropServices import Marshal

xlDirecDown = System.Enum.Parse(Excel.XlDirection, "xlDown")
xlDirecRight = System.Enum.Parse(Excel.XlDirection, "xlToRight")


class Lst_Xls():
	def __init__(self, path):
		self.lsta = []
		ex = Excel.ApplicationClass()
		ex.Visible = True
		lst_xls = []

		workbook = ex.Workbooks.Open(path)
		ws = workbook.Worksheets[1]
		
		#set the start row and start column
		self.fullrange = ws.Range[ws.Cells(startRow, startColumn), ws.Cells(startRow+len(colors), startColumn)]
		self.fullvalue = self.fullrange.Value2
		
				
	def setRowColorbyDict(self):
		index = 0
		for row in self.fullrange.Rows:
			if index == len(colors):
				break
			row.Interior.Color = eval("ColorTranslator.ToOle(Color.FromArgb(" + colors[index] + "))")
			index+=1
			
				
#Inputs
xlspath = IN[0]	
colors = IN[1]
startRow = IN[2]
startColumn = IN[3]

obj_xl_lst = Lst_Xls(xlspath)
obj_xl_lst.setRowColorbyDict()

OUT = str(len(colors))+' colors added to excel'
3 Likes