Excel color cells with python

Hi there!

I’m trying to color some cells in excel by python.

I was watching this topic in order to undertand how to do it:

It works fine, but just with two rows and I don’t know why.

Here the script where you can see that works with two rows:

Here the create dictionary node:

 excelRow =IN[0]
 dictcolor = {}
 for e in excelRow:
           dictcolor[str(e)]= "192,192,192"
 OUT =dictcolor

And here Paint cells:

 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, dictcolor):
 		self.dictcolor = dictcolor
 		self.lsta =[]
 		ex = Excel.ApplicationClass()
 		ex.Visible = True
 		lst_xls = []
 
 		workbook = ex.Workbooks.Open(path)
 		ws = workbook.Worksheets[1]
 		
 		##get number of Rows ##
 		rowCountF = ws.Columns[1].End(xlDirecDown).Row
 		##get number of Coloun  ##
 		colCountF = ws.Rows[1].End(xlDirecRight).Column
 		self.fullrange = ws.Range[ws.Cells(1, 1), ws.Cells(rowCountF, colCountF)]
 		self.fullvalue = self.fullrange.Value2
 		
 	def setRowColorbyDict(self):	
 		for row in self.fullrange.Rows:
 			rangerow = row.Value2
 			firstvalue = rangerow.GetValue(1,1)
 			if firstvalue in self.dictcolor:
 				strRgb = dictcolor.get(firstvalue)
 				row.Interior.Color = eval("ColorTranslator.ToOle(Color.FromArgb(" + strRgb + "))")
 				self.lsta.append([firstvalue, strRgb])				
 
 #dictionary to complete
 	
 xlspath = IN[0]
 
 dictcolor =IN[1]
 	
 obj_xl_lst = Lst_Xls(xlspath, dictcolor)
 obj_xl_lst.setRowColorbyDict()
 
 OUT = obj_xl_lst.lsta

Thks.

Manel

Please edit your code in right format. It is hard to read it.

1 Like

Did you see my last post in that topic? I am not sure what your end goal is but maybe my final script will help?

Sorry Deniz.

Done it!

Manel

Hi Steve,

Yes, I did. But I didn’t want to control rows and columns, I’m interested in color some rows with specific names.

The thing is that I didn’t know why it works just with two rows.

Thks

Manel

Can you provide a screenshot of your full graph and of the excel file you are trying to color?

 		##get number of Rows ##
 		rowCountF = ws.Columns[1].End(xlDirecDown).Row
 		##get number of Coloun  ##
 		colCountF = ws.Rows[1].End(xlDirecRight).Column
 		self.fullrange = ws.Range[ws.Cells(1, 1), ws.Cells(rowCountF, colCountF)]
 		self.fullvalue = self.fullrange.Value2

The section above reads the excel file and gets the count of rows and columns. Without seeing your data structure it is hard to say why its not working.

Finally I did it with you scripst because I couldn’t get a result with others options

Thks

Manel