Page 1 of 1
[Solved] Python copy text + style from a document to anothe
Posted: Thu Oct 01, 2015 10:54 am
by Bastien
Hi,
In a python macro I create a new doc from a template. I'd like now to copy the whole text of my current document (including styles) and paste it at the end of my new created document.
For now, I only can select text and paste it but I loose the formatting style. Is there a way to make it ?
Re: Python select text including style from a document to an
Posted: Thu Oct 01, 2015 12:00 pm
by Villeroy
Record a copy&paste operation in Basic, change the syntax to Python.
Otherwise, there is an XTransferable interface
http://www.openoffice.org/api/docs/comm ... rable.html
Re: Python select text including style from a document to an
Posted: Thu Oct 01, 2015 2:44 pm
by Bastien
What do you mean by record a copy&paste operation in Basic? I can't see this function.
The Xtransferable interface seems to be really powerful but it is quite abstract to me. Could you suggest me a way to use it (preferably in python)?
Re: Python select text including style from a document to an
Posted: Thu Oct 01, 2015 2:59 pm
by Villeroy
Ah, LibreOffice. menu:Tools>Options>Advanced>Enable Macro recording
Tools>Macros>Record macro ... (a new toolbar pops up)
Copy some cells, paste some cells, stop the macro recorder, save and edit the macro which is a dispatch macro. Dispatch macros simulate selections and clicks in the user interface. It is easy to convert dispatch macros from Basic to Python. ThisComponent --> XSCRIPTCONTEXT.getDocument(), change the blocks to Pyhon indentation blocks.
search.php?keywords=XTransferable&terms ... mit=Search
Re: Python select text including style from a document to an
Posted: Thu Oct 01, 2015 3:48 pm
by Bastien
Very quick, very easy indeed using the dispatcher.
Here is the code doing that:
Code: Select all
def copy_all():
document = XSCRIPTCONTEXT.getDocument()
ctx = uno.getComponentContext()
sm = ctx.ServiceManager
dispatcher = sm.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)
frame = document.CurrentController.Frame
dispatcher.executeDispatch(frame, ".uno:SelectAll", "", 0, ())
dispatcher.executeDispatch(frame, ".uno:Copy", "", 0, ())
It surely would have been smarter using Xtransferable but for now, it works.
Re: [Solved] Python copy text + style from a document to ano
Posted: Thu Oct 01, 2015 9:23 pm
by Villeroy