tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / DocumentHandling / DocumentConverter.py
blob4613a8b797e25ce0d008ca925a8699042781c989
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import argparse
11 import os
12 import sys
13 from os.path import abspath, basename, isdir, join, splitext
15 import uno
16 from com.sun.star.beans import PropertyValue
17 from com.sun.star.connection import NoConnectException
19 PROG = "$OFFICE_PROGRAM_PATH/python {}".format(basename(sys.argv[0]))
20 SOFFICE_CONNECTION_URI = "uno:socket,host=localhost,port=2083;urp;StarOffice.ComponentContext"
23 def connect_soffice():
24 """Connect to remote running LibreOffice"""
25 local_context = uno.getComponentContext()
26 resolver = local_context.ServiceManager.createInstanceWithContext(
27 "com.sun.star.bridge.UnoUrlResolver", local_context
29 try:
30 remote_context = resolver.resolve(SOFFICE_CONNECTION_URI)
31 except NoConnectException:
32 raise Exception("Cannot establish a connection to LibreOffice.")
34 return remote_context.ServiceManager.createInstanceWithContext(
35 "com.sun.star.frame.Desktop", remote_context
39 def convert(src_file, dest_file, to_type):
40 src_url = "file://{}".format(src_file).replace("\\", "/")
41 dest_url = "file://{}".format(dest_file).replace("\\", "/")
43 soffice = connect_soffice()
44 doc = soffice.loadComponentFromURL(
45 src_url, "_blank", 0, (PropertyValue(Name="Hidden", Value=True),)
48 opts = (
49 PropertyValue(Name="Overwrite", Value=True),
50 PropertyValue(Name="FilterName", Value=to_type),
52 try:
53 doc.storeAsURL(dest_url, opts)
54 finally:
55 doc.dispose()
58 def is_dir(value):
59 if not isdir(value):
60 raise argparse.ArgumentTypeError("{} is not a directory.".format(value))
61 return value
64 def main():
65 parser = argparse.ArgumentParser(description="Document Converter", prog=PROG)
66 parser.add_argument("from_dir",
67 type=is_dir,
68 help="Convert documents searched from this directory recursively")
69 parser.add_argument("to_type", help="Type to convert to, example: MS Word 97.")
70 parser.add_argument("extension",
71 help="Extension of the converted document, examples: doc, docx")
72 parser.add_argument("output_dir",
73 type=is_dir,
74 help="Converted document is stored into this directory")
76 args = parser.parse_args()
78 for dir_path, dir_names, file_names in os.walk(args.from_dir):
79 for name in file_names:
80 src_file = join(abspath(dir_path), name)
81 dest_file = "{}.{}".format(join(args.output_dir, splitext(name)[0]), args.extension)
82 convert(src_file, dest_file, args.to_type)
83 print("Converted", src_file, "to", dest_file)
86 if __name__ == "__main__":
87 main()
90 # vim: set shiftwidth=4 softtabstop=4 expandtab: