Insert textrange from hidden document, including formatting

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Induane
Posts: 28
Joined: Fri Mar 28, 2014 4:49 am

Insert textrange from hidden document, including formatting

Post by Induane »

I'm working within the Python UNO api, and attempting to insert a specific text range from a document I opened as hidden into the current document without using the clipboard (for a number of reasons, but mostly that I don't want to blow away the users current clipboard, and another issue that is beyond the scope of this question).

Basically I have a document open as hidden that I need to grab a specific paragraph from. In this case it's a piece of legislative statute with a specific section number that is being amended. I then create a paragraph enumeration and find the portion I need to import into the current document.

Code: Select all

# self.x_model is my currently opened document model

# Import all the styles from the source document
get.styles_from_doc(self.x_model, "some/file/path.odt")

# Open the source document as hidden
hidden_doc = get.hidden_document(self.x_model, "some/file/path.odt")

#  ... at this point some stuff happens such that I have the variable "ins_range" which is the
# text I need to import into the current document. I also have the cursor position "ins_pos" 
# of the current document where I need to insert this range.

# The following would be pseudo-code - there is no such method afaik. What should I do here?
self.x_model.insert_range(position=ins_pos, range=ins_range)
I need to preserve all the formatting that makes sense (page style problably doesn't but the rest would I'd imagine). What can I do here?
OpenOffice 3.1 on Windows 7 / LibreOffice 3.6 on Ubuntu 13.10 / LibreOffice 4.1 on Ubuntu 13.10
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Insert textrange from hidden document, including formatt

Post by Charlie Young »

I've tried similar things before, and I don't think this can be done easily. The source document being hidden is irrelevant.

The problem is "insertTextContent" won't accept a TextContent from a different document.

One approach is to create a new TextContent in the destination document, set its string to that of the source, Insert it, then use cursors to copy the formatting character-by-character (or whatever level of detail is necessary) to the destination.

I hope someone has a better way.
Apache OpenOffice 4.1.1
Windows XP
Post Reply