工作上最近在开发LibreOffice相关功能,目的是为了实现Word文件模板功能,即甲方制定好合同模板,我们再用代码将模板内部分内容替换或填充。技术调研之后选择了LibreOffice,优点是在线实时编辑,过程可视化。
前端通过iframe实现实时预览和编辑,通过PostMessage实现与后端交互,PostMessage调用Python Macro实现核心功能。同时多层iframe实现严格权限管控。
执行流程

开发环境
此次不再描述Python环境安装。系统为Windows11
安装Libreoffice本地客户端
下载地址:https://www.libreoffice.org/download/download-libreoffice/
安装开发工具
在此处下载插件,并通过文件安装:https://extensions.libreoffice.org
- APSO,建议装1.4.2版本
https://extensions.libreoffice.org/en/extensions/show/apso-alternative-script-organizer-for-python
- MRI
https://extensions.libreoffice.org/en/extensions/show/mri-uno-object-inspection-tool


编写代码
windows系统的Python Macro应该存放在这个文件夹,没有可自行创建
C:\Users\violet\AppData\Roaming\LibreOffice\4\user\Scripts\python
在该文件夹下创建.py文件
随便写一点点代码
def QueryAll():
"""
查询所有书签
"""
doc = XSCRIPTCONTEXT.getDocument()
bookmarks = doc.getBookmarks()
bookmark_names = bookmarks.getElementNames()
filtered_bookmarks = [
bk_name
for bk_name in bookmark_names
if not bk_name.startswith("_") and ":" not in bk_name
]
result = {"text": [], "table": []}
existing_table_indices = {}
for bk_name in filtered_bookmarks:
result["text"].append(bk_name)
return result
本地调试

找到需要执行的方法,进入Debug


持续更新
0