Python libreoffice 使用 uno 设置边距值、最佳高度和打印文档

Python libreoffice set margin values, optimal height and print document with uno

我试图通过集成在 Libreoffice 安装中的 Python 与 libreoffice 交互。而且我在任何地方都找不到如何在 PageStyle 中设置边距,以设置最佳行高并打印几份文档。或者,也许,我可以在 Libreoffice 中编写宏,然后 运行 从 python 编写宏。以下代码无效。

pageStyle = document.getStyleFamilies().getByName("PageStyles")
page = pageStyle.getByName("Default")
page.LeftMargin = 500

P.S。对不起我的英语。

在大多数版本的 LibreOffice 中,默认样式的名称是 "Default Style"。在 Apache OpenOffice 中,它被命名为 "Default"。

这是完整的代码。例如,将文件命名为 change_settings.py.

import uno

def set_page_style_margins():
    document = XSCRIPTCONTEXT.getDocument()
    pageStyle = document.getStyleFamilies().getByName("PageStyles")
    page = pageStyle.getByName("Default Style")
    page.LeftMargin = 500

g_exportedScripts = set_page_style_margins,

在我的 Windows 10 系统上,此脚本位于目录 C:\Users\<your username>\AppData\Roaming\LibreOffice\user\Scripts\python 下。您将需要创建最后两个目录,并且大小写必须匹配。

现在,在 LibreOffice Writer 中,转到 工具 -> 宏 -> 运行 宏。 展开到 我的宏 -> change_settings 和 select 宏名称 set_page_style_margins.

有关 Python LibreOffice 的完整介绍: