tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / Text / GraphicsInserter.py
blob635f03e2527612392e27eae8200436f6fddd8664
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 sys
12 import traceback
13 from os.path import isfile, dirname, join
15 import officehelper
16 from com.sun.star.beans import PropertyValue
17 from com.sun.star.text.TextContentAnchorType import AT_PARAGRAPH
19 LOG_FILE = join(dirname(__file__), "log.txt")
22 def insert_graphic(filename):
23 remote_context = officehelper.bootstrap()
24 srv_mgr = remote_context.getServiceManager()
25 desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)
27 doc_url = "private:factory/swriter"
28 doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple())
30 log_file = open(LOG_FILE, "w")
32 text = doc.getText()
33 cursor = text.createTextCursor()
35 try:
36 graphic = doc.createInstance("com.sun.star.text.TextGraphicObject")
37 except Exception:
38 traceback.print_exc(file=log_file)
39 return
41 log_file.write("inserting graphic\n")
42 try:
43 text.insertTextContent(cursor, graphic, True)
44 except Exception:
45 print("Could not insert Content")
46 traceback.print_exc()
47 return
49 log_file.write("adding graphic\n")
50 try:
51 graphic_url = f"file://{filename}".replace("\\", "/")
52 print("insert graphic: %s", graphic_url)
53 graphic_provider = srv_mgr.createInstanceWithContext(
54 "com.sun.star.graphic.GraphicProvider", remote_context
56 loaded_graphic = graphic_provider.queryGraphic(
57 (PropertyValue(Name="URL", Value=graphic_url),)
60 # Setting the graphic url
61 graphic.setPropertyValue("Graphic", loaded_graphic)
63 # Set properties for the inserted graphic
64 graphic.setPropertyValue("AnchorType", AT_PARAGRAPH)
65 # Setting the horizontal position
66 graphic.setPropertyValue("HoriOrientPosition", 5500)
67 # Setting the vertical position
68 graphic.setPropertyValue("VertOrientPosition", 4200)
69 # Setting the width
70 graphic.setPropertyValue("Width", 4400)
71 # Setting the height
72 graphic.setPropertyValue("Height", 4000)
73 except Exception:
74 print("Couldn't set property 'GraphicURL'")
75 traceback.print_exc(file=log_file)
77 log_file.close()
80 def is_file(value):
81 if not isfile(value):
82 raise argparse.ArgumentTypeError(f"File {value} is not an image file.")
83 return value
86 def main():
87 parser = argparse.ArgumentParser()
88 parser.add_argument("image", type=is_file, help="Path to an image file.")
89 args = parser.parse_args()
90 try:
91 insert_graphic(args.image)
92 except Exception:
93 traceback.print_exc()
94 sys.exit(1)
97 if __name__ == "__main__":
98 main()
100 # vim: set shiftwidth=4 softtabstop=4 expandtab: