summaryrefslogtreecommitdiff
path: root/ishtar_common/libreoffice.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-10-08 10:53:50 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-10-08 10:53:50 +0200
commit347a069fcd2274f5ae2c31dfde35b65fa492257a (patch)
tree0664cc84fc29c98af096dce8d0acc53a09a0e35e /ishtar_common/libreoffice.py
parent2f526ba22f183ef744cfd7ac7284585f8b318d95 (diff)
downloadIshtar-347a069fcd2274f5ae2c31dfde35b65fa492257a.tar.bz2
Ishtar-347a069fcd2274f5ae2c31dfde35b65fa492257a.zip
✨ document templates: manage export in PDF, HTML, xlsx, docx using LO unoconv
Diffstat (limited to 'ishtar_common/libreoffice.py')
-rw-r--r--ishtar_common/libreoffice.py30
1 files changed, 25 insertions, 5 deletions
diff --git a/ishtar_common/libreoffice.py b/ishtar_common/libreoffice.py
index 3869ca81a..573d99361 100644
--- a/ishtar_common/libreoffice.py
+++ b/ishtar_common/libreoffice.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
@@ -8,15 +8,37 @@ from com.sun.star.beans import PropertyValue
from com.sun.star.connection import NoConnectException
from com.sun.star.sheet.ValidationType import LIST
+# nosec: filename used is generated and sanitized
+import subprocess # nosec
+
#from com.sun.star.table import CellRangeAddress, CellAddress
-from ishtar_common.utils import num2col
+from ishtar_common.utils import num2col, sanitize_filepath
from django.conf import settings
RETRY = 5
+def get_connection():
+ return "socket,host={},port={};urp".format(
+ settings.LIBREOFFICE_HOST, settings.LIBREOFFICE_PORT
+ )
+
+
+def convert_document(document, destination_format):
+ if not document.lower().endswith(".ods") and not document.lower().endswith(".odt"):
+ return document
+ document = sanitize_filepath(document)
+ connection = get_connection() + ";StarOffice.ComponentContext"
+ args = [settings.UNOCONV_BINARY, "--connection", connection, "-f", destination_format,
+ document]
+ # nosec: filename is generated and sanitized
+ popen = subprocess.Popen(args) # nosec
+ popen.wait(timeout=20)
+ dest_document = document[:-4] + "." + destination_format
+ return dest_document
+
class UnoClient:
def __init__(self):
@@ -30,9 +52,7 @@ class UnoClient:
resolver = local_context.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", local_context)
- connection = "socket,host={},port={};urp".format(
- settings.LIBREOFFICE_HOST, settings.LIBREOFFICE_PORT
- )
+ connection = get_connection()
try:
self.service_manager = resolver.resolve(
"uno:{};StarOffice.ServiceManager".format(connection))