Сopy charts from different ODS (calc) into one ODP (impress)

Creating a macro - Writing a Script - Using the API

Сopy charts from different ODS (calc) into one ODP (impress)

Postby polusha » Sun Feb 17, 2019 8:53 pm

Help me please.
I need to copy charts from different ODS (calc) and ODP into one ODP (impress)
The code for copying from different ODP (impress) to one ODP (impress) is below.
But I can't copy from calc in this way.
I would be very grateful for any help!
PS Sorry for my bad english.

Code: Select all   Expand viewCollapse view
import sys
import os
import uno
import pyuno


def mergeFromOdpURL(desk, dispatcher, doc1, url, dt=1.0, number_slide=[0,]):
    ctr1 = doc1.getCurrentController()
    frame1 = ctr1.Frame

    doc2 = desk.loadComponentFromURL(url, "_blank", 0, ())
    ctr2 = doc2.getCurrentController()
    frame2 = ctr2.Frame

    dispatcher.executeDispatch(frame1, ".uno:DiaMode", "", 0, ())
    dispatcher.executeDispatch(frame2, ".uno:DiaMode", "", 0, ())

    for i in number_slide:
        page2 = doc2.DrawPages.getByIndex(i)
        ctr2.setCurrentPage(page2)
        dispatcher.executeDispatch(frame2, ".uno:Copy", "", 0, ())
        page1 = doc1.DrawPages.getByIndex(doc1.DrawPages.Count - 2)
        ctr1.setCurrentPage(page1)
        dispatcher.executeDispatch(frame1, ".uno:Paste", "", 0, ())

    dispatcher.executeDispatch(frame1, ".uno:DrawingMode", "", 0, ())
    doc2.close(True)



def fileNameToUrl(fname):
    if fname[0:7] != 'file://':
        fname = os.path.realpath(fname)
        return pyuno.systemPathToFileUrl(fname)
    else:
        return fname


def mergeFiles(ctx, desk, filelist, outname, numbers_slide):
    doc = desk.loadComponentFromURL(fileNameToUrl('default.odp'), "_blank", 0, ())
    dispatcher = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)
    for fname in filelist:
        mergeFromOdpURL(desk, dispatcher, doc, fileNameToUrl(fname), numbers_slide)
    # mergeFromURLExcel(desk, dispatcher, doc, fileNameToUrl(filelist[1]), 1)
    doc.DrawPages.remove(doc.DrawPages.getByIndex(1))
    saveProperty = uno.getTypeByName("com.sun.star.beans.PropertyValue")
    saveProperty.Name = "MS PowerPoint 97"
    saveProperty.Value = True
    doc.storeAsURL(fileNameToUrl(outname),())#('presentation', 'ppt', 'ppt', 'Microsoft PowerPoint 97/2000/XP', 'MS PowerPoint 97'))
    doc.close(False)


def asService():
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
    ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
    desk = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
    infiles = [
                "bubbles.odp",
            ]
    outfile = "output.odp"
    numbers_slide = [1,1]
    mergeFiles(ctx, desk, infiles, outfile, numbers_slide)


if __name__ == '__main__':
    asService()


But from calc it is impossible to adapt
OpenOffice 3.1 ?? Windows Vista / NeoOffice 2.2.3 ?? MacOS 10.4 / OpenOffice 2.4 ?? Ubuntu 9.04
polusha
 
Posts: 9
Joined: Sun Feb 17, 2019 8:35 pm

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby Lupp » Mon Feb 18, 2019 11:48 am

polusha wrote:But from calc it is impossible to adapt.
If you know that already, what use in asking?

This looks as if you expect helpers to "reverse-engineer" or "disassemble" your code to get the needed information about what you actually want to achieve in detail. This won't work.

You will have to explicate your intentions in clear words. You surely know (e.g.) that nobody can paste any lot of objects simply "into one ODP". Every single object at least needs a target position ... It's your turn to describe that, and also to specify in what way the respective objects should be selected from the source files. Once again: It's your job.

For any single process of Copy/Paste you will have to use the clipboard anyway, because a .odg or a .odp cannot accept a Chart (e.g.) that was connected to DataRanges in Calc without changing "the flavour". External data linkage is not supported by Charts. This afflicts Writer documents in the same way: The data from 'DataRanges' need to be written into internal 'DataTable' of the Chart on the fly and in the background between "Copy" and "Paste". It works, basically, however.

Having got the needed information some contributor here may try to offer more specific help.
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2462
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby polusha » Mon Feb 18, 2019 2:52 pm

But from calc it is impossible to adapt.

I can't do this. But I think that I don't understand just how it works. I'm a newbie.

Task:
1) I need from 1 calc sheet to copy 1 graph (first) and paste it on the 1st slide. Impress

How I try to do it
Code: Select all   Expand viewCollapse view
import sys
import os
import time
import uno
import pyuno

def mergeFromExcelURL():
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
    ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
    desk = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

    # ODP
    doc1 = desk.loadComponentFromURL(fileNameToUrl('default.odp'), "_blank", 0, ())
    dispatcher = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)

    DrawPageODP = doc1.DrawPages.getByIndex(0)
    ShapeODP = DrawPageODP.getByIndex(0)

    ctr1 = doc1.getCurrentController()
    frame1 = ctr1.Frame

    #ODS
    doc2 = desk.loadComponentFromURL(fileNameToUrl('bubble.ods'), "_blank", 0, ())
    ctr2 = doc2.getCurrentController()
    frame2 = ctr2.Frame

    dispatcher.executeDispatch(frame1, ".uno:DiaMode", "", 0, ())
    dispatcher.executeDispatch(frame2, ".uno:DiaMode", "", 0, ())

    ChartSheet = doc2.Sheets.getByName('Chart')
    DrawPage = ChartSheet.DrawPage
    ChartShape = DrawPage.getByIndex(0)
    Diagram = ChartShape.Model.Diagram
    EmbObj = ChartShape.EmbeddedObject


    #SElect2
    ctr2.select(ChartShape) # This is not true most likely. probably it is necessary to choose a diagram, but it does not work. ????????????????????
    dispatcher.executeDispatch(frame2, ".uno:Copy", "", 0, ())

    # SElect1
    ctr1.setCurrentPage(DrawPageODP) # What is there to choose? ????????????????????
    dispatcher.executeDispatch(frame1, ".uno:Paste", "", 0, ())

    dispatcher.executeDispatch(frame1, ".uno:DrawingMode", "", 0, ()) #?????????????????????????

    outfile = "output.odp"
    doc1.storeAsURL(fileNameToUrl(outfile), ())
    doc1.close(False)
    doc2.close(True)
OpenOffice 3.1 ?? Windows Vista / NeoOffice 2.2.3 ?? MacOS 10.4 / OpenOffice 2.4 ?? Ubuntu 9.04
polusha
 
Posts: 9
Joined: Sun Feb 17, 2019 8:35 pm

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby polusha » Mon Feb 18, 2019 6:52 pm

Does nobody know?
Can someone suggest in the right direction?
OpenOffice 3.1 ?? Windows Vista / NeoOffice 2.2.3 ?? MacOS 10.4 / OpenOffice 2.4 ?? Ubuntu 9.04
polusha
 
Posts: 9
Joined: Sun Feb 17, 2019 8:35 pm

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby RoryOF » Mon Feb 18, 2019 6:56 pm

Why do you use a macro for this? Why not make a screenshot of the chart(s) and /Insert /Picture : from file into the presentation file?
Apache OpenOffice 4.1.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 28912
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby polusha » Mon Feb 18, 2019 7:07 pm

I need the diagram to remain editable in Impress.
OpenOffice 3.1 ?? Windows Vista / NeoOffice 2.2.3 ?? MacOS 10.4 / OpenOffice 2.4 ?? Ubuntu 9.04
polusha
 
Posts: 9
Joined: Sun Feb 17, 2019 8:35 pm

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby Zizi64 » Mon Feb 18, 2019 9:01 pm

I need the diagram to remain editable in Impress.



I supposed that you want insert the chart as a static picture. But you want modify the chart, and you want edit the datarange in the future.
You have at least three options:
1.: If you want edit the datarange in Calc cells, then you must embed a full Calc file into impress as OLE object.
2.: If you want edit the datarange manually, then you must use the Insert - Chart (manually or programatically.
3.: And you can use the Insert Object - OLE object - LibreOffice Chart method.
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.4; AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7964
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby Lupp » Mon Feb 18, 2019 11:48 pm

A Chart inserted into a Calc document is an OLE object itself. I'm afraid there is no way to insert a chart into a spreadsheet model which itself is inserted as an OLE object into a Draw or Impress document. At least I don't know a way. If the Chart is pre-inserted content of a spreadsheet inserted in turn as OLE "from file" into a Draw document it is not editable as far as I can tell.

Via dispatch commands a Chart can first be copied (.uno:Copy) from a spreadsheet, and then Pasted into a Writer/Draw/Impress document (.uno:Paste). It will be an editable OLE object in the new place then. However, it has no longer any connection to spreadsheet ranges as its data source. It is re-defined containing the data itself in a DataTable object.

User code demonstrating the Copy/Paste from a spreadsheet into a Draw document for one Chart is contained in the attached example file.
copyChartFromCalcToDraw_0.ods
(15.94 KiB) Downloaded 41 times


===Edit 2019-02-19 13:45 (UTC + 01)===
If someone wants to try the code contained in the attachment above:

There is an issue (bug) with the Basic IDE showing in this case (as also sometimes in others).
The code works as expected if called via the menu (>Tools>Macros>Run).
If called interacting with the IDE the code fails in AOO 4.1.5 and in LibO 6.2.0 (e.g.) as well. In LibO it even may cause a crash under stepwise execution.
Last edited by Lupp on Tue Feb 19, 2019 2:46 pm, edited 1 time in total.
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2462
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Сopy charts from different ODS (calc) into one ODP (impr

Postby RoryOF » Tue Feb 19, 2019 12:01 am

How are you going to manipulate the diagram, supposing that this is possible? I think Impress may not be the application for this - I suggest you look at some of the computerised white (or, if old-fashioned like me) black boarding applications. I found some information that may be helpful at
https://www.techjunkie.com/how-to-turn-your-computer-screen-into-a-digital-whiteboard/
Apache OpenOffice 4.1.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 28912
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 7 guests