[Solved] [Python][Writer] Insert a bookmark with text?
Posted: Mon Apr 05, 2021 7:32 pm
Hello, all,
I try to insert bookmarks using Python in my document. I read this article: https://wiki.openoffice.org/wiki/Docume ... /Bookmarks , but it does not show how to bind some text to bookmark. I write this in my code:
So I insert text "1.1. " before my starting bookmark, and then I insert the first bookmark with name "task_1_1". But the text of this bookmark is empty, as you can check in "Insert -> Bookmark..." dialog. And I need some text there to create a reference in another document part. I create these references with the following code:
I use "Above/Below" style now, but I want to use "Reference", for which I need a bookmark with some text. So I repeat my question: how can I make a bookmark with some text? So that "Name" and "Text" columns for my bookmarks were filled (now only "Name" is filled).
I try to insert bookmarks using Python in my document. I read this article: https://wiki.openoffice.org/wiki/Docume ... /Bookmarks , but it does not show how to bind some text to bookmark. I write this in my code:
Code: Select all
task_num = str(part_index) + "." + str(subpart_index) + ". "
tasks_part_cursor = tasks_part_doc.Text.createTextCursor()
tasks_part_cursor.gotoEnd(False)
task_bookmark = tasks_part_doc.createInstance("com.sun.star.text.Bookmark")
task_bookmark.Name = "task_" + str(part_index) + "_" + str(subpart_index) # some numbers to provide a unique bookmark
tasks_part_doc.Text.insertString(tasks_part_cursor, task_num, False)
tasks_part_doc.Text.insertTextContent(tasks_part_cursor, task_bookmark, False)
Code: Select all
from com.sun.star.text.ReferenceFieldPart import TEXT as REF_TEXT, UP_DOWN as REF_UP_DOWN
from com.sun.star.text.ReferenceFieldSource import BOOKMARK as SRC_BOOKMARK
...
solutions_part_cursor = current_solution_doc.Text.createTextCursor()
solutions_part_cursor.gotoEnd(False)
solution_getref = current_solution_doc.createInstance("com.sun.star.text.textfield.GetReference")
solution_getref.SourceName = "task_" + str(part_index) + "_" + str(subpart_index)
solution_getref.ReferenceFieldSource = SRC_BOOKMARK
solution_getref.ReferenceFieldPart = REF_UP_DOWN
solution_getref.CurrentPresentation = "Task reference"
current_solution_doc.Text.insertTextContent(solutions_part_cursor, solution_getref, False)