Search code examples
python-3.xlibreoffice-calc

python3 and libre office calc - setting column width


I am using pyoo to generate reports as open document spreadsheets. pyoo can do everything I require bar setting column widths. Some I want to set as a constant, others as optimal width. From the pyoo website (https://github.com/seznam/pyoo): "If some important feature missing then the UNO API is always available."

A few hours of Googling got me to the class com.sun.star.table.TableColumn which from this page appears to have the properties ("Width" and "OptimalWidth") that I require, but -

>>> x = uno.getClass('com.sun.star.table.TableColumn')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/uno.py", line 114, in getClass
    return pyuno.getClass(typeName)
uno.RuntimeException: pyuno.getClass: uno exception com.sun.star.table.TableColumn is unknown

I have no idea whatsoever how to get this to work. The documentation for UNO is overwelming to say the least...

Any clues would be enormously appreciated.


Solution

  • Example Python-UNO code:

    def resize_spreadsheet_columns():
        oSheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)
        oColumns = oSheet.getColumns()
        oColumn = oColumns.getByName("B")
        oColumn.IsVisible = False
        oColumn = oColumns.getByName("C")
        oColumn.Width = 7000
        oColumn = oColumns.getByName("D")
        oColumn.OptimalWidth = True
    

    Documentation:

    EDIT:

    From the comment, it sounds like you need to work through an introductory tutorial on Python-UNO. Try http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html.