[Solved] [Python][Writer] Insert a bookmark with text?

Keyboard macros or custom scripts

[Solved] [Python][Writer] Insert a bookmark with text?

Postby yijinger » 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:

Code: Select all   Expand viewCollapse view
            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)


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:

Code: Select all   Expand viewCollapse view
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)


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).
Last edited by yijinger on Tue Apr 06, 2021 8:55 pm, edited 1 time in total.
LibreOffice 6.0.7.3 on Linux Mint 20
yijinger
 
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

Re: [Python][Writer] Insert a bookmark with text?

Postby FJCC » Mon Apr 05, 2021 8:43 pm

I'm not sure exactly what your goal is. Is this similar to what you want to do? It inserts some text, a bookmark and then text following the bookmark at the beginning of the document.
Code: Select all   Expand viewCollapse view
def setBookmark():
  doc = XSCRIPTCONTEXT.getDocument()
  oText = doc.getText()
  oCurs = oText.createTextCursor()
  oCurs.String = "1.1. "
  BMark = doc.createInstance("com.sun.star.text.Bookmark")
  BMark.setName("FirstName")
  oText.insertTextContent(oCurs.End, BMark, 0)
  oAnchor = BMark.getAnchor()
  oAnchor.setString("Text for 1.1. section")
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 8172
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Python][Writer] Insert a bookmark with text?

Postby yijinger » Mon Apr 05, 2021 10:28 pm

Yes, thank you very much, I found this "oAnchor.setString(...)", and it sets text for my bookmarks, but somewhy texts do not appear in references. My references are just empty fields. I revised all bookmarks after compiling the whole document, but nothing changed, references are still empty. So - how can I refresh them?

I found "oDoc.refresh()" method, and it looks like designed just for references, but it does nothing for me. After calling it my reference fields are empty.

And after I set a string with "oAnchor.setString()" for a bookmark, I expected to see the same string with "oAnchor.getString()", but I see just an empty string. What's wrong with these text fields?

I replaced bookmarks with reference marks, and nothing changed. The same empty fields. Yes, I know that I always can return to "UP_DOWN" ("Above/Below") reference parts, but I want to show my own text in fields.
Last edited by yijinger on Mon Apr 05, 2021 11:54 pm, edited 1 time in total.
LibreOffice 6.0.7.3 on Linux Mint 20
yijinger
 
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am

Re: [Python][Writer] Insert a bookmark with text?

Postby FJCC » Mon Apr 05, 2021 11:42 pm

It seems that the setString() method does not actually set the string of the bookmark's anchor. The text gets inserted but if I look at the bookmark afterwards, its String is still empty. That explains why your references are empty. I will look into this some more to see if I can find a way around that behavior.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 8172
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Python][Writer] Insert a bookmark with text?

Postby FJCC » Tue Apr 06, 2021 3:54 pm

Try this
Code: Select all   Expand viewCollapse view
def setBookmark():
  doc = XSCRIPTCONTEXT.getDocument()
  oText = doc.getText()
  oCurs = oText.createTextCursor()
  oCurs.String = "1.1. "
  oCurs.collapseToEnd()
  oCurs.setString("Text for 1.1. section")
  BMark = doc.createInstance("com.sun.star.text.Bookmark")
  BMark.setName("FirstName")
  oText.insertTextContent(oCurs, BMark, 1)
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 8172
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Python][Writer] Insert a bookmark with text?

Postby yijinger » Tue Apr 06, 2021 8:54 pm

Yes! It works. I begin to understand power of cursors.

Here is my final code, I insert a bookmark into a hidden text section, so that I can refer to this bookmark and get somewhere around this section:

Code: Select all   Expand viewCollapse view
            outer_section_cursor = tasks_part_doc.Text.createTextCursor()
            outer_section_cursor.gotoEnd(False)
            task_ref_section = tasks_part_doc.createInstance("com.sun.star.text.TextSection")
            task_ref_section.Name = "section" + task_bookmark_name   # just unique name for this section
            tasks_part_doc.Text.insertTextContent(outer_section_cursor, task_ref_section, False)

            ref_cursor = tasks_part_doc.Text.createTextCursorByRange(task_ref_section.getAnchor())
            ref_cursor.setString("Back to task")
            task_bookmark = tasks_part_doc.createInstance("com.sun.star.text.Bookmark")
            task_bookmark.Name = task_bookmark_string       # readable name as long as it serves as a tooltip for reference
            tasks_part_doc.Text.insertTextContent(ref_cursor, task_bookmark, True)

            task_ref_section.IsVisible = False

...

            solution_getref = current_solution_doc.createInstance("com.sun.star.text.textfield.GetReference")
            solution_getref.SourceName = task_bookmark_string     # yes, the same variable as above
            solution_getref.ReferenceFieldSource = SRC_BOOKMARK
            solution_getref.ReferenceFieldPart = REF_TEXT
            current_solution_doc.Text.insertTextContent(solutions_part_cursor, solution_getref, True)


I still don't get the full difference between bookmarks and reference marks, though.
LibreOffice 6.0.7.3 on Linux Mint 20
yijinger
 
Posts: 12
Joined: Fri Apr 02, 2021 8:00 am


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 4 guests