1 import filecmp
2 import sys
3 import uno
4 from com.sun.star.beans import PropertyValue
5 from com.sun.star.connection import NoConnectException
6 from glob import glob
7 from os import mkdir, path
8 from shutil import rmtree
9 from subprocess import Popen
0 from tempfile import NamedTemporaryFile
1 from time import sleep
2
3 SOCKET = 'socket,host=localhost,port=8100,tcpNoDelay=1;urp;'
4 exitCode = 0
5
6 # Start von Libre Office
7 try:
8     app = Popen([
9        '/usr/lib/libreoffice/program/soffice',
0         '--headless',
1         '--accept=' + SOCKET
2     ])
3 except Exception as e:
4     raise Exception("LibreOffice konnte nicht gestartet werden: %s" % e.message)
5
6 if app.pid <= 0:
7     raise Exception('LibreOffice konnte nicht gestartet werden!')
8
9 # Aufbau der Verbindung zu Libre Office
0 context = uno.getComponentContext()
1 resolver = context.ServiceManager.createInstanceWithContext(
2     'com.sun.star.bridge.UnoUrlResolver',
3     context
4 )
5
6 n = 0
7 while n < 12:
8     try:
9         context = resolver.resolve(
0             'uno:' + SOCKET + 'StarOffice.ComponentContext'
1         )
2         break
3     except NoConnectException:
4         pass
5     sleep(0.5)
6     n += 1
7
8 desktop = context.ServiceManager.createInstanceWithContext(
9     'com.sun.star.frame.Desktop',
0     context
1 )
2
3 if not (desktop):
4     raise Exception('Der Libre-Office-Desktop konnte nicht erzeugt werden!')
5
6 # Ab hier wird Libre Office über die Verbindung gesteuert
7
8 # Initaler "Hallo Welt" Test
9 document = desktop.loadComponentFromURL(
0     'private:factory/swriter',
1     '_blank',
2     0,
3     ()
4 )
5 cursor = document.Text.createTextCursor()
6 cursor.ParaStyleName = 'Heading 1'
7 document.Text.insertString(cursor, 'Hallo Welt!', 0)
8
9 txtFile = NamedTemporaryFile('w+', encoding='utf-8-sig')
0
1 textFilter = PropertyValue()
2 textFilter.Name = 'FilterName'
3 textFilter.Value = 'Text'
4
5 document.storeToURL('file://' + txtFile.name, (textFilter,))
6
7 text = txtFile.read()
8 if (text != 'Hallo Welt!\n'):
9     print("FAIL: \"Hallo Welt\"-Test fehlgeschlagen! Das Resultat war: %s" % text)
0     exitCode = 1
1 else:
2     print('OK: "Hallo Welt!" wird exportiert wie erwartet.')
3
4 document.close(False)
5
6 # Vorbereiten des Test- und Ergebnisordners
7 if sys.argv[1] and path.isdir(sys.argv[1]):
8     testdir = path.abspath(sys.argv[1])
9     resultdir = path.join(testdir, 'Ergebnis_Dateien')
0     if path.isdir(resultdir):
1         rmtree(resultdir)
2     mkdir(resultdir, 0o700)
3
4     # Durchlauf der Testdokumente
5     pdfFilter = PropertyValue()
6     pdfFilter.Name = 'FilterName'
7     pdfFilter.Value = 'writer_pdf_Export'
8
9     testfiles = glob(path.join(testdir, 'Test_*'))
0     for testfile in testfiles:
1
2         document = desktop.loadComponentFromURL(
3             'file://' + testfile,
4             '_blank',
5             0,
6             ()
7         )
8
9         resultFile =  path.basename(testfile)[5:-4]
0
1         fullPath = 'file://' + path.join(resultdir, 'PDF_' + resultFile + '.pdf')
2         document.storeToURL(fullPath, (pdfFilter,))
3
4         textResultFile = path.join(resultdir, 'Text_' + resultFile + '.txt')
5         fullPath = 'file://' + textResultFile
6         document.storeToURL(fullPath, (textFilter,))
7
8         expectationFile = path.join(testdir, 'Text_' + resultFile + '.txt')
9         if (path.isfile(expectationFile)):
0             if filecmp.cmp(textResultFile, expectationFile, False):
1                 print("OK: Datei %s wird exportiert wie erwartet." % testfile)
2             else:
3                 print("FAIL: Datei %s wird nicht exportiert wie erwartet!" % testfile)
4                 exitCode = 1
5         else:
6             print("--: Keine Vergleichsdatei für %s." % testfile)
7         document.close(False)
8
9
0 # Schließen von LibreOffice
1 desktop.terminate()
2
3 sleep(2)
4
5 app.wait()
6
7 sys.exit(exitCode)