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/.
13 from os
.path
import isfile
, dirname
, join
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")
33 cursor
= text
.createTextCursor()
36 graphic
= doc
.createInstance("com.sun.star.text.TextGraphicObject")
38 traceback
.print_exc(file=log_file
)
41 log_file
.write("inserting graphic\n")
43 text
.insertTextContent(cursor
, graphic
, True)
45 print("Could not insert Content")
49 log_file
.write("adding graphic\n")
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)
70 graphic
.setPropertyValue("Width", 4400)
72 graphic
.setPropertyValue("Height", 4000)
74 print("Couldn't set property 'GraphicURL'")
75 traceback
.print_exc(file=log_file
)
82 raise argparse
.ArgumentTypeError(f
"File {value} is not an image file.")
87 parser
= argparse
.ArgumentParser()
88 parser
.add_argument("image", type=is_file
, help="Path to an image file.")
89 args
= parser
.parse_args()
91 insert_graphic(args
.image
)
97 if __name__
== "__main__":
100 # vim: set shiftwidth=4 softtabstop=4 expandtab: