pythonlibreofficelibreoffice-calcpyuno

How to change the LineWidth of a cell border using PyUNO in LibreOffice Calc?


I am writing a Python script to automatically adjust cell borders in LibreOffice Calc. I think I know what property I need to change, however when I assign a new value to this property, the value does not change.

For instance, I wrote this code to change the TopLine.LineWidth of a single Cell from 0 to 10.

# Access the current calc document
model = desktop.getCurrentComponent()
# Access the active sheet
active_sheet = model.CurrentController.ActiveSheet
# Get the cell and change the value of LineWidth
cell = active_sheet.getCellByPosition(2, 2)
cell.TableBorder2.TopLine.LineWidth = 10

I don't get any errors after running this code. And I have also made sure that I am accessing the cell I wish to modify. However, this code does not change the cell's border width.

I tried doing some debugging by printing the value before and after the assignment:

# This first print statement returns 0 because the cell has no borders
print(cell.TableBorder2.TopLine.LineWidth)
cell.TableBorder2.TopLine.LineWidth = 10
# This second print statement still returns 0, but I was expecting it to return 10
print(cell.TableBorder2.TopLine.LineWidth)

Does anyone know what I am doing wrong?


Solution

  • You need to set the cell property to a changed border object. From https://ask.libreoffice.org/en/question/145885/border-macro-no-longer-works/:

    aThinBorder = oRange.TopBorder2
    aThinBorder.LineWidth = 1
    oRange.TopBorder2 = aThinBorder