| View previous topic :: View next topic |
| Author |
Message |
Lobbezoo General User


Joined: 02 Apr 2005 Posts: 7 Location: Aix-en-Provence, France
|
Posted: Wed Apr 20, 2005 1:05 am Post subject: passing documents AND macro parameters. |
|
|
HI,
Since the subject is going under a bit in the other topic on this, I thought I'd rease it in a separate thread.
Here is the problem:
From a lokal python program, i am selecting some items in a database, and after that starting OOo with a document - telecharged from my central server (via a hhtp URL). So far so good.
Now, i need a macro (IN THIS DOCUMENT) that autostarts after loading, and get the rest of the data from a cental MYSQL database and fill in a page for the user to print x times.
This allso seems to be no problem.
HOWEVER......
I have to find a way to pass the first arguments (a variable number of strings = keys) to the macro. This seems to be not possible. OOo does not accept to be started from python with a document AND some parameters.
It does work, however if I type it in on the command line. Under python the spaces in the comand are replaced by %20, and the whoile thing interpreted as a documentname (which, of course, does not exists).
the string i'm sending is : "program/swriter -o ksjsdksd old.sxw 'macro:///Main.Module1.DB_ShowParams(test 1, test 2)'" or something similar. It than becomes "program/swriter ksjsdksd%20old.sxw%20 'macro:///Main.Module1.DB_ShowParams(test%201,%20test%202)'
Now, i've red that with some mecanism of interprocess communication this could be done, but I thing that's overkill for such a 'simple' problem. (Don't want to learn an other system yet )
And, since it seems to work when i give in the above command from the shell (bash) command line, there must be a solution to allso do it from python.
Anybody ?
Steven, who would be extremely grateful for a solution !  |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Wed Apr 20, 2005 7:14 am Post subject: Re: passing documents AND macro parameters. |
|
|
I'm not sure I fully understand your previous problem, and I'm not sure I would have a solution for it.
| Lobbezoo wrote: | Now, i've red that with some mecanism of interprocess communication this could be done, but I thing that's overkill for such a 'simple' problem. (Don't want to learn an other system yet ) |
But let me tell you more about that interprocess communication thing. It's not so hard as you think.
Two major objectives....
- Configure and then run OOo on computer A so that it is listening for UNO connections
- Run a Python program on computer B which connects to computer A and makes that OOo do something by pulling its puppet strings via. the API
Please note that computer A and B do NOT need to be running the same version of OOo, nor do they even need to be running the same operating system!
Computer A
Here are some past threads that will help accomplish the first part.
Configuring to listen for UNO connections
http://www.oooforum.org/forum/viewtopic.php?p=12370#12370
http://www.oooforum.org/forum/viewtopic.php?t=9768
* UnoConnectionListener
http://www.oooforum.org/forum/viewtopic.php?t=3754
The easiest way to configure computer A is to use my UnoConnectionListener macro document which you can download from OOoMacros.org. Open the document, and configure OOo to listen. Be sure to change "localhost" to "0" so that OOo will accept a connection from any other computer.
Now that the OOo on computer A is configured to listen for a remote connection, make sure that OOo is running on computer A.
Computer B
Now, go to computer B. Computer B must also have OOo installed. Inside of the OOo on computer B is a python. It is important that you use the Python included within OOo because a standard Python cannot execute the
| Code: | import uno
import unohelper |
statements.
In your OOo installation find this....
OOo/program/python.bat
(This may be a shell script on non-windows platforms.)
Run that batch file. A python interpreter appears.
Now take the following program, find the line that says
| Code: | # CHANGE ME!!!
# CHANGE ME!!!
# CHANGE ME!!!
# Connect to running office on a different computer.
getServiceManager( cHost="localhost", cPort="8100" )
|
and change it so that instead of localhost, it connects to Computer A that you previously configured.
Now find this line of code
| Code: | # CHANGE ME!!!
# CHANGE ME!!!
# CHANGE ME!!!
# Prepare the filename to save.
# We're going to save the file in several different formats,
# but all based on the same filename.
cFile = "C:\Documents and Settings\dbrewer\Desktop\MyCalc" # Windows
#cFile = "/home/danny/Desktop/MyCalc.sxc" # Linux
|
Change to a suitable pathname so that documents will be created on your desktop.
Now run the python program.
| Code: | | import CalcExample.py |
Over on Computer A the following will happen.
- A Calc spreadsheet will be created
- Some glowing sales figures will be put into the spreadsheet
- A graph of the sales figures will be added to the spreadsheet
- The spreadsheet will be printed, using three different techniques (so you might get three printouts)
- The spreadsheet will be saved to your desktop in a number of formats (i.e. Excel, PDF, CSV, DIF, SYLK, HTML).
- The spreadsheet will be closed! (To prevent this comment out the line that says oDoc.close( True ). In Python use a # character to comment out the line.)
Now here is the example program.
| Code: | import string
import uno
# The ServiceManager of the running OOo.
# It is cached in a global variable.
goServiceManager = False
def getServiceManager( cHost="localhost", cPort="8100" ):
"""Get the ServiceManager from the running OpenOffice.org.
Then retain it in the global variable goServiceManager for future use.
This is similar to the GetProcessServiceManager() in OOo Basic.
"""
global goServiceManager
if not goServiceManager:
# Get the uno component context from the PyUNO runtime
oLocalContext = uno.getComponentContext()
# Create the UnoUrlResolver on the Python side.
oLocalResolver = oLocalContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", oLocalContext )
# Connect to the running OpenOffice.org and get its context.
oContext = oLocalResolver.resolve( "uno:socket,host=" + cHost + ",port=" + cPort + ";urp;StarOffice.ComponentContext" )
# Get the ServiceManager object
goServiceManager = oContext.ServiceManager
return goServiceManager
# CHANGE ME!!!
# CHANGE ME!!!
# CHANGE ME!!!
# Connect to running office on a different computer.
getServiceManager( cHost="localhost", cPort="8100" )
# This is the same as ServiceManager.createInstance( ... )
def createUnoService( cClass ):
"""A handy way to create a global objects within the running OOo.
Similar to the function of the same name in OOo Basic.
"""
oServiceManager = getServiceManager()
oObj = oServiceManager.createInstance( cClass )
return oObj
# The StarDesktop object. (global like in OOo Basic)
# It is cached in a global variable.
StarDesktop = None
def getDesktop():
"""An easy way to obtain the Desktop object from a running OOo.
"""
global StarDesktop
if StarDesktop == None:
StarDesktop = createUnoService( "com.sun.star.frame.Desktop" )
return StarDesktop
# preload the StarDesktop variable.
getDesktop()
# The CoreReflection object.
# It is cached in a global variable.
goCoreReflection = False
def getCoreReflection():
global goCoreReflection
if not goCoreReflection:
goCoreReflection = createUnoService( "com.sun.star.reflection.CoreReflection" )
return goCoreReflection
def createUnoStruct( cTypeName ):
"""Create a UNO struct and return it.
Similar to the function of the same name in OOo Basic.
"""
oCoreReflection = getCoreReflection()
# Get the IDL class for the type name
oXIdlClass = oCoreReflection.forName( cTypeName )
# Create the struct.
oReturnValue, oStruct = oXIdlClass.createObject( None )
return oStruct
def makePropertyValue( cName=None, uValue=None, nHandle=None, nState=None ):
"""Create a com.sun.star.beans.PropertyValue struct and return it.
"""
oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
if cName != None:
oPropertyValue.Name = cName
if uValue != None:
oPropertyValue.Value = uValue
if nHandle != None:
oPropertyValue.Handle = nHandle
if nState != None:
oPropertyValue.State = nState
return oPropertyValue
def makePoint( nX, nY ):
"""Create a com.sun.star.awt.Point struct."""
oPoint = createUnoStruct( "com.sun.star.awt.Point" )
oPoint.X = nX
oPoint.Y = nY
return oPoint
def makeSize( nWidth, nHeight ):
"""Create a com.sun.star.awt.Size struct."""
oSize = createUnoStruct( "com.sun.star.awt.Size" )
oSize.Width = nWidth
oSize.Height = nHeight
return oSize
def makeRectangle( nX, nY, nWidth, nHeight ):
"""Create a com.sun.star.awt.Rectangle struct."""
oRect = createUnoStruct( "com.sun.star.awt.Rectangle" )
oRect.X = nX
oRect.Y = nY
oRect.Width = nWidth
oRect.Height = nHeight
return oRect
def Array( *args ):
"""This is just sugar coating so that code from OOoBasic which
contains the Array() function can work perfectly in python."""
tArray = ()
for arg in args:
tArray += (arg,)
return tArray
def convertToURL( cPathname ):
"""Convert a Windows or Linux pathname into an OOo URL."""
if len( cPathname ) > 1:
if cPathname[1:2] == ":":
cPathname = "/" + cPathname[0] + "|" + cPathname[2:]
cPathname = string.replace( cPathname, "\\", "/" )
cPathname = "file://" + cPathname
return cPathname
def CalcExample():
# create a new Calc spreadsheet.
oDoc = StarDesktop.loadComponentFromURL( "private:factory/scalc", "_blank", 0, Array() )
#-----
# Use this instead to open an EXISTING calc document,
# and assign it to variable oDoc.
# cFile = "C:\Documents and Settings\danny\Desktop\MyCalc" # Windows
# cFile = "/home/danny/Desktop/MyCalc.sxc" # Linux
# cURL = convertToURL( cFile + ".sxc" )
# oDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0, Array() )
#-----
# Here are two ways to get access to one of the various sheets
# in the spreadsheet document.
# Note that these don't make the sheet *vislble*, they merely give
# access to the sheet's content within the program.
oSheet = oDoc.getSheets().getByIndex( 0 ) # get the zero'th sheet
#oSheet = oDoc.getSheets().getByName( "Sheet3" ) # get by name
#-----
# Put some sales figures onto the sheet.
oSheet.getCellByPosition( 0, 0 ).setString( "Month" )
oSheet.getCellByPosition( 1, 0 ).setString( "Sales" )
oSheet.getCellByPosition( 2, 0 ).setString( "End Date" )
oSheet.getCellByPosition( 0, 1 ).setString( "Jan" )
oSheet.getCellByPosition( 0, 2 ).setString( "Feb" )
oSheet.getCellByPosition( 0, 3 ).setString( "Mar" )
oSheet.getCellByPosition( 0, 4 ).setString( "Apr" )
oSheet.getCellByPosition( 0, 5 ).setString( "May" )
oSheet.getCellByPosition( 0, 6 ).setString( "Jun" )
oSheet.getCellByPosition( 0, 7 ).setString( "Jul" )
oSheet.getCellByPosition( 0, 8 ).setString( "Aug" )
oSheet.getCellByPosition( 0, 9 ).setString( "Sep" )
oSheet.getCellByPosition( 0, 10 ).setString( "Oct" )
oSheet.getCellByPosition( 0, 11 ).setString( "Nov" )
oSheet.getCellByPosition( 0, 12 ).setString( "Dec" )
oSheet.getCellByPosition( 1, 1 ).setValue( 3826.37 )
oSheet.getCellByPosition( 1, 2 ).setValue( 3504.21 )
oSheet.getCellByPosition( 1, 3 ).setValue( 2961.45 )
oSheet.getCellByPosition( 1, 4 ).setValue( 2504.12 )
oSheet.getCellByPosition( 1, 5 ).setValue( 2713.98 )
oSheet.getCellByPosition( 1, 6 ).setValue( 2248.17 )
oSheet.getCellByPosition( 1, 7 ).setValue( 1802.13 )
oSheet.getCellByPosition( 1, 8 ).setValue( 2003.22 )
oSheet.getCellByPosition( 1, 9 ).setValue( 1502.54 )
oSheet.getCellByPosition( 1, 10 ).setValue( 1207.68 )
oSheet.getCellByPosition( 1, 11 ).setValue( 1319.71 )
oSheet.getCellByPosition( 1, 12 ).setValue( 786.03 )
oSheet.getCellByPosition( 2, 1 ).setFormula( "=DATE(2004;01;31)" )
oSheet.getCellByPosition( 2, 2 ).setFormula( "=DATE(2004;02;29)" )
oSheet.getCellByPosition( 2, 3 ).setFormula( "=DATE(2004;03;31)" )
oSheet.getCellByPosition( 2, 4 ).setFormula( "=DATE(2004;04;30)" )
oSheet.getCellByPosition( 2, 5 ).setFormula( "=DATE(2004;05;31)" )
oSheet.getCellByPosition( 2, 6 ).setFormula( "=DATE(2004;06;30)" )
oSheet.getCellByPosition( 2, 7 ).setFormula( "=DATE(2004;07;31)" )
oSheet.getCellByPosition( 2, 8 ).setFormula( "=DATE(2004;08;31)" )
oSheet.getCellByPosition( 2, 9 ).setFormula( "=DATE(2004;09;30)" )
# Note that these last three dates are not set as DATE() function calls.
oSheet.getCellByPosition( 2, 10 ).setFormula( "10/31/2004" )
oSheet.getCellByPosition( 2, 11 ).setFormula( "11/30/2004" )
oSheet.getCellRangeByName( "C13" ).setFormula( "12/31/2004" )
#-----
#-----
# Format the date cells as dates.
com_sun_star_util_NumberFormat_DATE = uno.getConstantByName( "com.sun.star.util.NumberFormat.DATE" )
oFormats = oDoc.getNumberFormats()
oLocale = createUnoStruct( "com.sun.star.lang.Locale" )
nDateKey = oFormats.getStandardFormat( com_sun_star_util_NumberFormat_DATE, oLocale )
oCell = oSheet.getCellRangeByName( "C2:C13" )
oCell.NumberFormat = nDateKey
#-----
#-----
# Now add a chart to the spreadsheet.
oCellRangeAddress = oSheet.getCellRangeByName( "A1:B13" ).getRangeAddress()
# oCellRangeAddress = MakeCellRangeAddress( 0, 0, 1, 1, 12 )
# Get the collection of charts from the sheet.
oCharts = oSheet.getCharts()
# Add a new chart with a specific name,
# in a specific rectangle on the drawing page,
# and connected to specific cells of the spreadsheet.
oCharts.addNewByName( "Sales",
makeRectangle( 8000, 1000, 16000, 10000 ),
Array( oCellRangeAddress ),
True, True )
# From the collection of charts, get the new chart we just created.
oChart = oCharts.getByName( "Sales" )
# Get the chart document model.
oChartDoc = oChart.getEmbeddedObject()
# Get the drawing text shape of the title of the chart.
oTitleTextShape = oChartDoc.getTitle()
# Change the title.
oTitleTextShape.String = "Sales Chart"
# Create a diagram.
oDiagram = oChartDoc.createInstance( "com.sun.star.chart.BarDiagram" )
# Set its parameters.
oDiagram.Vertical = True
# Make the chart use this diagram.
oChartDoc.setDiagram( oDiagram )
# Ask the chart what diagram it is using.
# (Unnecessary, since variable oDiagram already contains this value.)
oDiagram = oChartDoc.getDiagram()
# Make more changes to the diagram.
oDiagram.DataCaption = uno.getConstantByName( "com.sun.star.chart.ChartDataCaption.VALUE" )
oDiagram.DataRowSource = uno.getConstantByName( "com.sun.star.chart.ChartDataRowSource.COLUMNS" )
#
#-----
#-----
# Now demonstrate how to manipulate the sheets.
# Insert six more sheets into the document.
nNumSheetsCurrently = oDoc.getSheets().getCount()
oDoc.getSheets().insertNewByName( "Fred", nNumSheetsCurrently+1 )
oDoc.getSheets().insertNewByName( "Joe", nNumSheetsCurrently+2 )
oDoc.getSheets().insertNewByName( "Bill", nNumSheetsCurrently+3 )
oDoc.getSheets().insertNewByName( "Sam", nNumSheetsCurrently+4 )
oDoc.getSheets().insertNewByName( "Tom", nNumSheetsCurrently+5 )
oDoc.getSheets().insertNewByName( "David", nNumSheetsCurrently+6 )
# Now find a sheet named "Sheet2" and get rid of it.
oDoc.getSheets().removeByName( "Sheet2" )
# Now find the sheet named "Sam" and change its name to "Sheet 37"
oDoc.getSheets().getByName( "Sam" ).Name = "Sheet 37"
#
#-----
#-------
# Now print the document -- three different ways.
# Technique 1.
# Now print the document.
# Print two copies.
# Print pages 1 thru 4, and also page 10.
#
# NOTE: we would do it like this, except the word "print"
# has a special meaning in python, and cannot be invoked
# as a method.
#oDoc.print(
# Array(
# makePropertyValue( "CopyCount", 2 ),
# makePropertyValue( "Pages", "1-4;10" ) ) )
uno.invoke( oDoc, "print", ( Array(
makePropertyValue( "CopyCount", 2 ),
makePropertyValue( "Pages", "1-4;10" ) ), ) )
# Technique 2.
# Print the document already, without any arguments.
uno.invoke( oDoc, "print", ( Array(), ) )
#oDoc.print( Array() )
# Using technique 1 or 2, be sure not to close the document
# until printing is completed.
# http://www.oooforum.org/forum/viewtopic.php?p=23144#23144
# Technique 3.
# Print the document by bringing up the Print Job dialog box
# for the user to interact with.
oDocFrame = oDoc.getCurrentController().getFrame()
oDispatchHelper = createUnoService( "com.sun.star.frame.DispatchHelper" )
oDispatchHelper.executeDispatch( oDocFrame, ".uno:Print", "", 0, Array() )
# To learn some more about the dispatcher, see these articles...
# http://www.oooforum.org/forum/viewtopic.php?t=5058
# http://www.oooforum.org/forum/viewtopic.php?t=5057
#
#-------
#-------
# Now save the document
# CHANGE ME!!!
# CHANGE ME!!!
# CHANGE ME!!!
# Prepare the filename to save.
# We're going to save the file in several different formats,
# but all based on the same filename.
cFile = "C:\Documents and Settings\dbrewer\Desktop\MyCalc" # Windows
#cFile = "/home/danny/Desktop/MyCalc.sxc" # Linux
# Now save the spreadsheet in native OOo Calc format.
cURL = convertToURL( cFile + ".sxc" )
oDoc.storeAsURL( cURL, Array() )
# Note the above used storeAsUrl,
# the following use storeToUrl.
# Now save it in Excel format.
cURL = convertToURL( cFile + ".xls" )
oDoc.storeToURL( cURL, Array( makePropertyValue( "FilterName", "MS Excel 97" ) ) )
# Now save a PDF.
cURL = convertToURL( cFile + ".pdf" )
oDoc.storeToURL( cURL, Array( makePropertyValue( "FilterName", "calc_pdf_Export" ) ) )
# Now save it in CSV format.
cURL = convertToURL( cFile + ".csv" )
oDoc.storeToURL( cURL, Array( makePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ) ) )
# Now save it in DIF format.
cURL = convertToURL( cFile + ".dif" )
oDoc.storeToURL( cURL, Array( makePropertyValue( "FilterName", "DIF" ) ) )
# Now save it in SYLK format.
cURL = convertToURL( cFile + ".sylk" )
oDoc.storeToURL( cURL, Array( makePropertyValue( "FilterName", "SYLK" ) ) )
# Now save as HTML.
cURL = convertToURL( cFile + ".html" )
oDoc.storeToURL( cURL, Array( makePropertyValue( "FilterName", "HTML (StarCalc)" ) ) )
# A list of some filter names you can use for both loading
# and saving a document can be found here...
# http://www.oooforum.org/forum/viewtopic.php?t=3549
#
#-------
#-------
# Now close the document
oDoc.close( True )
#-------
CalcExample()
|
The above Python program is completely self contained. I adapted it from this example that I posted earlier.
See also....
Danny's Python Modules
http://www.oooforum.org/forum/viewtopic.phtml?t=14409
This thread has
- lots of python code for manipiulating OOo
- pointers to instructions on how to set up OOo's built in python to work with IDLE (on Windows)
- pointers to lots of other Python / OOo articles here on OOoForum
A lot of introductory information / answers about OOo UNO and Python
http://www.oooforum.org/forum/viewtopic.phtml?p=75468#75468 _________________ Want to make OOo Drawings like the colored flower design to the left?
Last edited by DannyB on Sat Apr 23, 2005 9:06 am; edited 1 time in total |
|
| Back to top |
|
 |
Lobbezoo General User


Joined: 02 Apr 2005 Posts: 7 Location: Aix-en-Provence, France
|
Posted: Fri Apr 22, 2005 7:18 am Post subject: |
|
|
Thanks a lot for this very enlighting reply.
In the mean time i made a version where the data i need in the form is written to a textfile, which is accessed as a database.
This, because i find the contruction of access direct to a mysql db via java EXTREMELY complicated. Also the behavour that it asks for a password euch time e session is opened or hangs to long without any activity is very annoying.
I'm sure the solution You proposed is the best one on the long run, but with a lack of time for everything )) I had to move on.
Thanks a lot anyway.
STeven |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
ungalee Newbie

Joined: 08 Mar 2010 Posts: 1
|
Posted: Wed Mar 10, 2010 10:31 pm Post subject: |
|
|
Sign Language Interpreters Please help? I am really intrested in being a Sign Language Interpreter I'm really intrested in ASL. But... I don't know if I just want to be an inerpreter for school and things like that. Is there a special job where you can be just a music/concerts intpreter???
_________________________
affiliateelite ~ affiliateelite.com ~ adgooroo ~ adgooroo.com |
|
| Back to top |
|
 |
|
|
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
|