summaryrefslogtreecommitdiffstats
path: root/odk/examples/python/Text/GraphicsInserter.py
blob: 3d09aedd5a13427416cd65883f2082d4ae34b310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import argparse
import sys
import traceback
from os.path import isfile, dirname, join

import officehelper
from com.sun.star.beans import PropertyValue
from com.sun.star.text.TextContentAnchorType import AT_PARAGRAPH

LOG_FILE = join(dirname(__file__), "log.txt")


def insert_graphic(filename):
    remote_context = officehelper.bootstrap()
    srv_mgr = remote_context.getServiceManager()
    desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", remote_context)

    doc_url = "private:factory/swriter"
    doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple())

    log_file = open(LOG_FILE, "w")

    text = doc.getText()
    cursor = text.createTextCursor()

    try:
        graphic = doc.createInstance("com.sun.star.text.TextGraphicObject")
    except:
        traceback.print_exc(file=log_file)
        return

    log_file.write("inserting graphic\n")
    try:
        text.insertTextContent(cursor, graphic, True)
    except:
        print("Could not insert Content")
        traceback.print_exc()
        return

    log_file.write("adding graphic\n")
    try:
        graphic_url = f"file://{filename}".replace("\\", "/")
        print("insert graphic: %s", graphic_url)
        graphic_provider = srv_mgr.createInstanceWithContext(
            "com.sun.star.graphic.GraphicProvider", remote_context
        )
        loaded_graphic = graphic_provider.queryGraphic(
            (PropertyValue(Name="URL", Value=graphic_url),)
        )

        # Setting the graphic url
        graphic.setPropertyValue("Graphic", loaded_graphic)

        # Set properties for the inserted graphic
        graphic.setPropertyValue("AnchorType", AT_PARAGRAPH)
        # Setting the horizontal position
        graphic.setPropertyValue("HoriOrientPosition", 5500)
        # Setting the vertical position
        graphic.setPropertyValue("VertOrientPosition", 4200)
        # Setting the width
        graphic.setPropertyValue("Width", 4400)
        # Setting the height
        graphic.setPropertyValue("Height", 4000)
    except:
        print("Couldn't set property 'GraphicURL'")
        traceback.print_exc(file=log_file)

    log_file.close()


def is_file(value):
    if not isfile(value):
        raise argparse.ArgumentTypeError(f"File {value} is not an image file.")
    return value


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("image", type=is_file, help="Path to an image file.")
    args = parser.parse_args()
    try:
        insert_graphic(args.image)
    except:
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    main()

# vim: set shiftwidth=4 softtabstop=4 expandtab: