Ghpython excel

Hi,
I’m trying to create border in excel by ghpython, but I can’t find any example or reference or libraries for this.
Any ideas ?

import clr
import csv
import os
clr.AddReference("Microsoft.Office.Interop.Excel")
from System.Runtime.InteropServices import Marshal
Excel = Marshal.GetActiveObject("Excel.Application")

workbook = Excel.ActiveWorkbook
worksheet = Excel.ActiveSheet

Excel.Worksheets(1).Range["C4:C10"].Value = 5

use openpyxl library
here’s the documentation

and the section responsible for styles (including borders)

it basically works like so

#import
import openpyxl
from openpyxl.styles import Border, Side

#---- 
# your code for opening the excel document
# assign the workbook to wb or any other variable
#----

#create a border style
border = Border(left=Side(border_style='thin', color='FF0000'),    # Red border
                right=Side(border_style='thin', color='FF0000'),
                top=Side(border_style='thin', color='FF0000'),
                bottom=Side(border_style='thin', color='FF0000'))

# Apply the border to the cells
wb['A1'].border = border
1 Like

and here’s a working gh file (in rhino 8)
xl borders with python.gh (4.1 KB)

for reference, this is the code

# r: openpyxl

import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Border, Side

# Load the existing Excel file
xlFile = load_workbook(xls)
wb = xlFile['Sheet1']

#create a border style
border = Border(left=Side(border_style='thick', color='FF0000'),    # Red border
                right=Side(border_style='thick', color='FF0000'),
                top=Side(border_style='thick', color='FF0000'),
                bottom=Side(border_style='thick', color='FF0000'))

# Apply the border to the cells
wb['A1'].border = border
wb['A2'].border = border

#save the file
xlFile.save(xls)

1 Like

Thank you, you save my day.

1 Like