为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 LibreOffice Calc 中使用 PyUNO 更改单元格边框的线宽?

如何在 LibreOffice Calc 中使用 PyUNO 更改单元格边框的线宽?

慕的地8271018 2023-08-22 15:00:50
我正在编写一个 Python 脚本来自动调整 LibreOffice Calc 中的单元格边框。我想我知道需要更改哪些属性,但是当我为该属性分配新值时,该值不会更改。例如,我编写了这段代码来将单个单元格的 TopLine.LineWidth 从 0 更改为 10。# Access the current calc documentmodel = desktop.getCurrentComponent()# Access the active sheetactive_sheet = model.CurrentController.ActiveSheet# Get the cell and change the value of LineWidthcell = active_sheet.getCellByPosition(2, 2)cell.TableBorder2.TopLine.LineWidth = 10运行此代码后我没有收到任何错误。我还确保我正在访问我想要修改的单元格。但是,此代码不会更改单元格的边框宽度。我尝试通过打印赋值前后的值来进行一些调试:# This first print statement returns 0 because the cell has no bordersprint(cell.TableBorder2.TopLine.LineWidth)cell.TableBorder2.TopLine.LineWidth = 10# This second print statement still returns 0, but I was expecting it to return 10print(cell.TableBorder2.TopLine.LineWidth)有谁知道我做错了什么?
查看完整描述

2 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

您需要将单元格属性设置为更改后的边框对象。

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


查看完整回答
反对 回复 2023-08-22
?
守着一只汪

TA贡献1872条经验 获得超3个赞

因此,经过大量研究后,我找到了至少三种更改边框设置的方法。因为我花了很多精力,所以我想我应该把它们留在这里,这样将来其他人可能会更容易找到答案。


在所有示例中,我将单个单元格的 TopBorder 的 LineWidth 设置为 10。


方法 1:使用 getPropertyValue() 和 setPropertyValue()


cell = active_sheet.getCellByPosition(1, 1)

border_prop = cell.getPropertyValue("TopBorder")

border_prop.LineWidth = 10

cell.setPropertyValue("TopBorder", border_prop)

方法2(源自Jim K的回答)


cell = active_sheet.getCellByPosition(1, 1)

border_prop = cell.TopBorder2

border_prop.LineWidth = 10

cell.TopBorder2 = border_prop

方法 3:使用 BorderLine2 结构体


border_prop = uno.createUnoStruct("com.sun.star.table.BorderLine2")

border_prop.LineWidth = 10

cell = active_sheet.getCellByPosition(1, 1)

cell.setPropertyValue("TopBorder", border_prop)


查看完整回答
反对 回复 2023-08-22
  • 2 回答
  • 0 关注
  • 1546 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信