server端:指定端口为2083
soffice --accept="socket,host=localhost,port=2083;urp;StarOffice.ServiceManager" --nologo --headless
client端:
#!/usr/bin/python3
import argparse
import os
import sys
from os.path import abspath, basename, isdir, join, splitext
import uno
from com.sun.star.beans import PropertyValue
from com.sun.star.connection import NoConnectException
import subprocess
import time
PORT = 2083
PROG = "$OFFICE_PROGRAM_PATH/python {}".format(basename(sys.argv[0]))
SOFFICE_CONNECTION_URI = "uno:socket,host=localhost,port=%d;urp;StarOffice.ComponentContext" % PORT
def startServer():
"""
Start a headless instance of OpenOffice.
"""
args = ['soffice',
'--accept=socket,host=localhost,port=%d;urp;StarOffice.ServiceManager' % PORT,
'--norestore',
'--nofirststartwizard',
'--nologo',
'--headless',
]
try:
# pid = os.spawn(os.P_NOWAIT, args[0], args,env)
subprocess.Popen(args, shell=False)
except Exception:
print("Cannot establish a connection to LibreOffice.")
def connect_soffice():
"""Connect to remote running LibreOffice"""
local_context = uno.getComponentContext()
resolver = local_context.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", local_context
)
startSevered = True
n=0
while n < 6:
try:
remote_context = resolver.resolve(SOFFICE_CONNECTION_URI)
break
except NoConnectException:
pass
if startSevered:
startServer()
startSevered = False
time.sleep(2)
n += 1
if n ==6:
return False
return remote_context
def createProp(name, value):
prop = PropertyValue()
prop.Name = name
prop.Value = value
return prop
def convert(src_file, dest_file, to_type,contents=False,contentTitle="Contents"):
src_url = "file:///{}".format(src_file).replace("\\", "/")
dest_url = "file:///{}".format(dest_file).replace("\\", "/")
context = connect_soffice()
soffice = context.ServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", context)
doc = soffice.loadComponentFromURL(src_url, "_blank", 0,
(PropertyValue(Name="Hidden", Value=True),)
# conversion_props
)
# 获取内容
text = doc.getText()
cursor = text.createTextCursor()
# 设置目录深度
if contents=="True":
index = doc.createInstance("com.sun.star.text.ContentIndex")
index.setPropertyValue("Level", 10)
index.setPropertyValue("CreateFromOutline", True)
# 设置目录标题
index.setPropertyValue("Title", contentTitle)
doc.getText().insertTextContent(cursor, index, False)
# doc.refresh()
# 目录更新
index.update()
# 插入目录
doc.storeToURL(dest_url,(createProp("FilterName",to_type),createProp("CharacterSet","UTF-8"),createProp("Overwrite",True)))
doc.dispose()
def is_dir(value):
if not isdir(value):
raise argparse.ArgumentTypeError("{} is not a directory.".format(value))
return value
def main():
parser = argparse.ArgumentParser(description="Document Converter", prog=PROG)
parser.add_argument("from_dir",
#type=is_dir,
help="Convert documents searched from this directory recursively")
parser.add_argument("to_type", help="Type to convert to, example: MS Word 97.")
parser.add_argument("extension",
help="Extension of the converted document, examples: doc, docx")
parser.add_argument("content",
help="Extension of the converted document, examples: doc, docx")
parser.add_argument("contentTitle",
help="Extension of the converted document, examples: doc, docx")
parser.add_argument("output_dir",
#type=is_dir,
help="Converted document is stored into this directory")
args = parser.parse_args()
convert(args.from_dir, args.output_dir, args.to_type, args.content,args.contentTitle)
if __name__ == "__main__":
main()