473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Solved: pyUNO Converter

Hi!

This script will convert files to PDF with OpenOffice.
Other formats (html, doc ...) should be possible, too.

Have a nice day,
thomas
# OpenOffice1.1 comes with its own python interpreter.
# This Script needs to be run with the python from OpenOffice.org:
# /opt/OpenOffice.org/program/python
# Start the Office before connecting:
# soffice "-accept=socket,host=localhost,port=2002;urp;"
#
# pyUNO Imports

import uno
from com.sun.star.beans import PropertyValue

# Python Imports

import os
import sys

# For a list of possible export formats see
# http://www.openoffice.org/files/docu...scription.html
# or
# /opt/OpenOffice.org/share/registry/data/org/openoffice/Office/TypeDetection.xcu

export_format="writer_pdf_Export"
export_extension="pdf"

def usage():
print """Usage: %s in_dir out_dir
All files in in_dir will be opened with OpenOffice.org and
saved to out_dir
You must start the office with this line before starting
this script:
soffice "-accept=socket,host=localhost,port=2002;urp;"
""" % (os.path.basename(sys.argv[0]))

def do_file(file, desktop, out_url):
# Load File
file=os.path.abspath(file)
url="file:///%s" % file
properties=[]
p=PropertyValue()
p.Name="Hidden"
p.Value=True
properties.append(p)
doc=desktop.loadComponentFromURL(
url, "_blank", 0, tuple(properties));
if not doc:
print "Failed to open '%s'" % file
return
# Save File
properties=[]
p=PropertyValue()
p.Name="Overwrite"
p.Value=True
properties.append(p)
p=PropertyValue()
p.Name="FilterName"
p.Value=export_format
properties.append(p)
p=PropertyValue()
p.Name="Hidden"
p.Value=True
basename=os.path.basename(file)
idx=basename.rfind(".")
assert(idx!=-1)
basename=basename[:idx]
url_save="%s/%s.%s" % (out_url, basename, export_extension)
try:
doc.storeToURL(
url_save, tuple(properties))
except:
print "Failed while writing: '%s'" % file
doc.dispose()

def main():
if len(sys.argv)!=3:
usage()
sys.exit(1)
in_dir=sys.argv[1]
out_dir=sys.argv[2]
out_url="file://%s" % os.path.abspath(out_dir)
print out_url
# Init: Connect to running soffice process
context = uno.getComponentContext()
resolver=context.ServiceManager.createInstanceWith Context(
"com.sun.star.bridge.UnoUrlResolver", context)
try:
ctx = resolver.resolve(
"uno:socket,host=localhost,port=2002;urp;StarOffic e.ComponentContext")
except:
print "Could not connect to running openoffice."
usage()
sys.exit()
smgr=ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame .Desktop",ctx)

files=os.listdir(in_dir)
files.sort()
for file in files:
print "Processing %s" % file
file=os.path.join(in_dir, file)
do_file(file, desktop, out_url)

if __name__=="__main__":
main()

Jul 18 '05 #1
4 3418
On Thu, 27 Nov 2003 17:41:07 +0100, Thomas Guettler wrote:
This script will convert files to PDF with OpenOffice. Other formats
(html, doc ...) should be possible, too.


Nice.

Is there any way to start soffice without a display?
Could be used to offer a webservice-documentconverter.

Murple
Jul 18 '05 #2
Am Fri, 28 Nov 2003 14:18:12 +0100 schrieb Andreas Kuntzagk:
On Thu, 27 Nov 2003 17:41:07 +0100, Thomas Guettler wrote:
This script will convert files to PDF with OpenOffice. Other formats
(html, doc ...) should be possible, too.


Nice.

Is there any way to start soffice without a display?
Could be used to offer a webservice-documentconverter.


No, but there is a special X-Server which does not need
a real display. But I don't think that it is thread save.

Untested:
You could start e.g. 5 office instances listening on 5
different ports and syncronize the access yourself.

Fortunately I only need it for batch processing.

thomas

Jul 18 '05 #3
"Thomas Guettler" <gu*****@thomas-guettler.de> wrote in message news:<pa****************************@thomas-guettler.de>...

Nice work!

One question though, doesn't this work only for OOo writer files (not
Draw or Calc files)?

I tried hacking it, and in the end, simply tried all output filters
that put out PDF in succession, until one worked.

I _know_ this is a hack, but I couldn't find any information on how to
use pyUNO to query OO and return what _type_ of file it had
opened.....

How _does_ one do that, anyway?

Thanks,
Duane

Hi!

This script will convert files to PDF with OpenOffice.
Other formats (html, doc ...) should be possible, too.

Have a nice day,
thomas
# OpenOffice1.1 comes with its own python interpreter.
# This Script needs to be run with the python from OpenOffice.org:
# /opt/OpenOffice.org/program/python
# Start the Office before connecting:
# soffice "-accept=socket,host=localhost,port=2002;urp;"
#
# pyUNO Imports

import uno
from com.sun.star.beans import PropertyValue

# Python Imports

import os
import sys

# For a list of possible export formats see
# http://www.openoffice.org/files/docu...scription.html
# or
# /opt/OpenOffice.org/share/registry/data/org/openoffice/Office/TypeDetection.xcu

export_format="writer_pdf_Export"
export_extension="pdf"

def usage():
print """Usage: %s in_dir out_dir
All files in in_dir will be opened with OpenOffice.org and
saved to out_dir
You must start the office with this line before starting
this script:
soffice "-accept=socket,host=localhost,port=2002;urp;"
""" % (os.path.basename(sys.argv[0]))

def do_file(file, desktop, out_url):
# Load File
file=os.path.abspath(file)
url="file:///%s" % file
properties=[]
p=PropertyValue()
p.Name="Hidden"
p.Value=True
properties.append(p)
doc=desktop.loadComponentFromURL(
url, "_blank", 0, tuple(properties));
if not doc:
print "Failed to open '%s'" % file
return
# Save File
properties=[]
p=PropertyValue()
p.Name="Overwrite"
p.Value=True
properties.append(p)
p=PropertyValue()
p.Name="FilterName"
p.Value=export_format
properties.append(p)
p=PropertyValue()
p.Name="Hidden"
p.Value=True
basename=os.path.basename(file)
idx=basename.rfind(".")
assert(idx!=-1)
basename=basename[:idx]
url_save="%s/%s.%s" % (out_url, basename, export_extension)
try:
doc.storeToURL(
url_save, tuple(properties))
except:
print "Failed while writing: '%s'" % file
doc.dispose()

def main():
if len(sys.argv)!=3:
usage()
sys.exit(1)
in_dir=sys.argv[1]
out_dir=sys.argv[2]
out_url="file://%s" % os.path.abspath(out_dir)
print out_url
# Init: Connect to running soffice process
context = uno.getComponentContext()
resolver=context.ServiceManager.createInstanceWith Context(
"com.sun.star.bridge.UnoUrlResolver", context)
try:
ctx = resolver.resolve(
"uno:socket,host=localhost,port=2002;urp;StarOffic e.ComponentContext")
except:
print "Could not connect to running openoffice."
usage()
sys.exit()
smgr=ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame .Desktop",ctx)

files=os.listdir(in_dir)
files.sort()
for file in files:
print "Processing %s" % file
file=os.path.join(in_dir, file)
do_file(file, desktop, out_url)

if __name__=="__main__":
main()

Jul 18 '05 #4
One could create the properties list in one go:

properties = [PropertyValue() for i in range(3)]

And then fill it in:

properties[0].Name = ...
properties[0].Value = ...
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Guettler | last post by:
Hi! The introduction on http://udk.openoffice.org/python/python-bridge.html is very good. Nice to see, that python is integrated into OpenOffice1.1. Has somone written a script which...
1
by: John Hunter | last post by:
Does anyone have any example scripts using the OpenOffince python-bridge module pyuno to load xls, extract the data, and/or save to another format such as xsc or csv. Thanks, JDH
7
by: Katja Süss | last post by:
Hi! maybe somebody can give me an hint to my problem setting up PyUNO on my Mac to work with my Python not the Python delivered with OpenOffice. As said in installation instructions I've set...
0
by: Sells, Fred | last post by:
I'm using windows xp and OpenOffice 2.0 and doing my first project with pyuno. I've got the basics to work,. An example I googled at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.