【问题标题】:python3 and libre office calc - setting column widthpython和libreoffice calc - 设置列宽
【发布时间】:2018-04-27 15:44:58
【问题描述】:

我正在使用 pyoo 将报告生成为打开的文档电子表格。 pyoo 可以做所有我需要的栏设置列宽。有些我想设置为常数,其他的设置为最佳宽度。来自 pyoo 网站 (https://github.com/seznam/pyoo):“如果缺少某些重要功能,则 UNO API 始终可用。”

几个小时的谷歌搜索让我找到了来自this page 的 com.sun.star.table.TableColumn 类,它似乎具有我需要的属性(“Width”和“OptimalWidth”),但是 -

>>> 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

我不知道如何让它工作。至少可以说,UNO 的文档非常受欢迎......

任何线索将不胜感激。

【问题讨论】:

    标签: python-3.x libreoffice-calc


    【解决方案1】:

    Python-UNO 代码示例:

    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
    

    文档:

    编辑

    从评论中,听起来您需要完成有关 Python-UNO 的介绍性教程。试试http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html

    【讨论】:

    • 谢谢,但是 XSCRIPTCONTEXT 不是只用于宏吗? - 我正在尝试使用外部 python 脚本控制正在运行的 calc 实例。
    【解决方案2】:

    感谢 Jim K,您为我指明了正确的方向,我成功了。我的 python 脚本生成了一个十张电子表格,手动调整列宽变得很痛苦。如果有人想对此发表评论或需要解决方案,我会在我的最终代码下方发布。在我看来,它结合了原始的 UNO 调用和 pyoo,就像猪的早餐,但我猜它有效。

    #! /usr/bin/python3.6
    
    import os, pyoo, time, uno
    
    s = '-'
    while s != 'Y':
        s = input("Have you remembered to start Calc? ").upper()
    
    os.system("soffice --accept=\"socket,host=localhost,port=2002;urp;\" --norestore --nologo --nodefault")
    time.sleep(2)
    desktop = pyoo.Desktop('localhost', 2002)
    doc = desktop.create_spreadsheet()
    
    class ofic:
        sheet_idx = 0
        row_num = 0
        sheet = None
    
    
    o = ofic()
    
    uno_localContext = uno.getComponentContext()
    uno_resolver = uno_localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", uno_localContext )
    uno_ctx = uno_resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
    uno_smgr = uno_ctx.ServiceManager
    uno_desktop = uno_smgr.createInstanceWithContext( "com.sun.star.frame.Desktop", uno_ctx)
    uno_model = uno_desktop.getCurrentComponent()
    uno_controller = uno_model.getCurrentController()
    uno_sheet_count = 0
    
    for i in range(5):
        doc.sheets.create("Page {}".format(i+1), index=o.sheet_idx)
        o.sheet = doc.sheets[o.sheet_idx]
        o.sheet[0, 0].value = "The quick brown fox jumps over the lazy dog"
        o.sheet[1, 1].value = o.sheet_idx
    
        uno_controller.setActiveSheet(uno_model.Sheets.getByIndex(uno_sheet_count))
        uno_sheet_count += 1
        uno_active_sheet = uno_model.CurrentController.ActiveSheet
        uno_columns = uno_active_sheet.getColumns()
        uno_column = uno_columns.getByName("A")
        uno_column.OptimalWidth = True
        uno_column = uno_columns.getByName("B")
        uno_column.Width = 1350
    
        o.sheet_idx += 1
    
    doc.save("whatever.ods")
    doc.close()
    

    【讨论】:

    • 看起来代码可以通过坚持纯UNO来简化。这也将消除对第三方 pyoo 库的依赖。就个人而言,我从未见过使用这些额外库的优势。此外,它们的文档记录从未像 UNO 那样完善。
    • 这是一个复杂的电子表格(实际上,该脚本最多可生成 11 个工作簿,每个工作簿中可能有 16 张工作表。大约有 700 行代码从数据库中提取数据,填充和格式化工作表。如果您批量添加内容,pyoo 应该会快得多。我认为理想的解决方案是将列宽功能添加到 pyoo。有一天我可能会考虑...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多