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

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
polusha
Posts: 9
Joined: Sun Feb 17, 2019 8:35 pm

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

Post by polusha »

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

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
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

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

Post by Lupp »

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 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
polusha
Posts: 9
Joined: Sun Feb 17, 2019 8:35 pm

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

Post by polusha »

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

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

Post by polusha »

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
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Post by RoryOF »

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.15 on Xubuntu 22.04.4 LTS
polusha
Posts: 9
Joined: Sun Feb 17, 2019 8:35 pm

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

Post by polusha »

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
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

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

Post by Zizi64 »

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; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
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
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

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

Post by Lupp »

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 159 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 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Post by RoryOF »

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- ... hiteboard/
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Post Reply