OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Impress, Python: change chart data [SOLVED]

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Mon Mar 19, 2007 8:42 am    Post subject: Impress, Python: change chart data [SOLVED] Reply with quote

Hello,

I have a OpenOffice Presentation. In slide one, there is a graphic/chart. I need to change, using Python-Uno, some cells content.

I have done it using OpenOffice Calc. But I don't know how to do it in Impress (get the chart reference and change chart data)

Any idea/help/pointer?

Thank you very much,

Carles


Last edited by cpina on Tue Mar 20, 2007 8:26 am; edited 1 time in total
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Mon Mar 19, 2007 9:14 am    Post subject: some news... Reply with quote

Using it:
target = pages.getByIndex(0)
print target.getByIndex(0).getPropertyValue("Model")

It seems that I am going to get the object that I need. But not yet, I think that I have to use OLE2 object?

I copy-paste the big code (that is a copy-paste from some web and I don't have the reference here, sorry):

Code:
#!/usr/bin/python

import uno

# Get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()

# Create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
                "com.sun.star.bridge.UnoUrlResolver", localContext )

# Connect to the running office
ctx = resolver.resolve(
  "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager

# Get the central desktop object
desktop = smgr.createInstanceWithContext(
    "com.sun.star.frame.Desktop",ctx)

# Pull the current document, we asssume this is an Impress doc
doc = desktop.getCurrentComponent()
frame = desktop.getCurrentFrame()
ctrl = frame.getController()  # In impress this implements XDrawView
pages = doc.getDrawPages()    # Page list of the current impress document

# Set to some page ...
target = pages.getByIndex(0)
print target.getByIndex(0).getPropertyValue("Model")

ctrl.setCurrentPage(target)


Of course, I have to startup OO:
ooffice "-accept=socket,host=localhost,port=2002;urp;" file_with_one_graphic.odp

Any new pointer of information? Smile
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Mon Mar 19, 2007 9:55 am    Post subject: Reply with quote

Parts of this might be interesting (Chart data without spreadsheet cells):
http://www.oooforum.org/forum/viewtopic.phtml?t=31620
You don't need a listening office when you save your *.py file in <user_dir>/Scripts/python/
You can call the macro just like a Basic macro and a special variable XSCRIPTCONTEXT refers to the right context. See HelloWorld example at Tools>Macros>Organize>Python...
_________________
Rest in peace, oooforum.org
Get help on https://forum.openoffice.org
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Mon Mar 19, 2007 11:43 am    Post subject: Reply with quote

Villeroy: thanks, i will check this link! Tomorrow (or today) I will try to write here how has worked.

I need a listening OO because this is part of process and is more convenient Smile, but thanks for the suggestion

Carles
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Mon Mar 19, 2007 11:51 am    Post subject: Reply with quote

villeoy: I have fast read that post and I think that is not my problem.

My problem is: how to change the data chart from Impress slide? I have the chart there, I can get the chart object but I don't know how to change the associated data. You can see how I am doing it in my initial post.

Later I will check the links linked from your linked post Smile, maybe I am lucky there.

Carles
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Tue Mar 20, 2007 2:12 am    Post subject: Reply with quote

Hi!

I have been able to access to chart data:

Code:
target = pages.getByIndex(0)
a=target.getByIndex(0).getPropertyValue("Model")
data=a.getData().getData()
print data


Result:
Code:
((9.0999999999999996, 3.2000000000000002, 4.54), (2.3999999999999999, 8.8000000000000007, 9.6500000000000004), (3.1000000000000001, 1.5, 3.7000000000000002), (4.2999999999999998, 9.0199999999999996, 6.2000000000000002))



But I am not able to change it! (XChartDocument hasn't setData, for example)

Any idea?

Thank you very much,

Carles
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Tue Mar 20, 2007 2:20 am    Post subject: Reply with quote

Wow! I could change the data-chart:

Code:
target = pages.getByIndex(0)
a=target.getByIndex(0).getPropertyValue("Model")
data=a.getData().getData()

print data
data2=((100.0999999999999996, 100.2000000000000002, 4.54), (2.3999999999999999, 8.8000000000000007, 9.6500000000000004), (3.1000000000000001, 1.5, 3.7000000000000002), (4.2999999999999998, 9.0199999999999996, 6.2000000000000002))
p=data
print a.getData().setData(data2)

But chart is not repainted with new data!
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Tue Mar 20, 2007 3:53 am    Post subject: Reply with quote

Are you shure? Did you read the Basic example?
Code:

import uno

def createUnoService(ctx, service):
    return ctx.ServiceManager.createInstance(service)

def getDesktop(ctx):
    return ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop",ctx)

def getCurrentComponent(ctx):
    return getDesktop(ctx).getCurrentComponent()

if __name__ == "__main__":
    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" )
    model = getCurrentComponent(ctx).DrawPages.getByIndex(0).getByIndex(0).Model
    modelData = model.getData()
    print modelData #...supportedInterfaces={com.sun.star.chart.XChartDataArray...
    data = modelData.getData()
    lstData = []
    lstRow = []
    for row in data:
        lstRow = list(row)
        for value in row:
            lstRow.append(value * 2)
        lstData.append(tuple(lstRow))

    modelData.setData(tuple(lstData))

_________________
Rest in peace, oooforum.org
Get help on https://forum.openoffice.org
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Tue Mar 20, 2007 7:32 am    Post subject: Reply with quote

Thanks Villeroy! Now it worked! It seems that before I didn't read correctly Neutral

But I still have a problem: if Impress is in presentation mode (F5, you know) the slide is not refreshed. I need to refresh it in presentation mode.

How I can do it?

One time more: thank you very much

Carles
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Tue Mar 20, 2007 7:42 am    Post subject: Reply with quote

Actually the OpenOffice chart is refreshed, but not the OpenOffice Slide Window (you know, at least in Linux, there is two windows: the OpenOffice itself one and "new window" with the presentation in full screen).

Any idea?

Thank you!
Back to top
View user's profile Send private message
cpina
General User
General User


Joined: 21 Mar 2005
Posts: 24

PostPosted: Tue Mar 20, 2007 8:25 am    Post subject: Reply with quote

I did it!

Last problem: Python script changing the chart is not reflected on presentation Window (full screen)

Solution: in "Slide transition", check "Advance slide" "Automatically after 1 sec". If there is more than one slide, it will do a loop (slide1, slide2, slide3, slide1, slide2, etc.) (I think). If there is only one slide, will reload/refresh this slide every 1 sec Smile and this makes the trick. There is not flicking or any other side effect.

Thanks again!
Back to top
View user's profile Send private message
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 10106
Location: Germany

PostPosted: Tue Mar 20, 2007 9:13 am    Post subject: Reply with quote

I was just about writing that I'm clueless. Thank you for finding the solution.
_________________
Rest in peace, oooforum.org
Get help on https://forum.openoffice.org
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group