python+libreoffice实现转换word,pdf,html等并生成目录

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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值