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,h ost=localhost,p ort=2002;urp;"
#
# pyUNO Imports
import uno
from com.sun.star.be ans 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.x cu
export_format=" writer_pdf_Expo rt"
export_extensio n="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,h ost=localhost,p ort=2002;urp;"
""" % (os.path.basena me(sys.argv[0]))
def do_file(file, desktop, out_url):
# Load File
file=os.path.ab spath(file)
url="file:///%s" % file
properties=[]
p=PropertyValue ()
p.Name="Hidden"
p.Value=True
properties.appe nd(p)
doc=desktop.loa dComponentFromU RL(
url, "_blank", 0, tuple(propertie s));
if not doc:
print "Failed to open '%s'" % file
return
# Save File
properties=[]
p=PropertyValue ()
p.Name="Overwri te"
p.Value=True
properties.appe nd(p)
p=PropertyValue ()
p.Name="FilterN ame"
p.Value=export_ format
properties.appe nd(p)
p=PropertyValue ()
p.Name="Hidden"
p.Value=True
basename=os.pat h.basename(file )
idx=basename.rf ind(".")
assert(idx!=-1)
basename=basena me[:idx]
url_save="%s/%s.%s" % (out_url, basename, export_extensio n)
try:
doc.storeToURL(
url_save, tuple(propertie s))
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.arg v[2]
out_url="file://%s" % os.path.abspath (out_dir)
print out_url
# Init: Connect to running soffice process
context = uno.getComponen tContext()
resolver=contex t.ServiceManage r.createInstanc eWithContext(
"com.sun.star.b ridge.UnoUrlRes olver", context)
try:
ctx = resolver.resolv e(
"uno:socket,hos t=localhost,por t=2002;urp;Star Office.Componen tContext")
except:
print "Could not connect to running openoffice."
usage()
sys.exit()
smgr=ctx.Servic eManager
desktop = smgr.createInst anceWithContext ("com.sun.star. frame.Desktop", ctx)
files=os.listdi r(in_dir)
files.sort()
for file in files:
print "Processing %s" % file
file=os.path.jo in(in_dir, file)
do_file(file, desktop, out_url)
if __name__=="__ma in__":
main()
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,h ost=localhost,p ort=2002;urp;"
#
# pyUNO Imports
import uno
from com.sun.star.be ans 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.x cu
export_format=" writer_pdf_Expo rt"
export_extensio n="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,h ost=localhost,p ort=2002;urp;"
""" % (os.path.basena me(sys.argv[0]))
def do_file(file, desktop, out_url):
# Load File
file=os.path.ab spath(file)
url="file:///%s" % file
properties=[]
p=PropertyValue ()
p.Name="Hidden"
p.Value=True
properties.appe nd(p)
doc=desktop.loa dComponentFromU RL(
url, "_blank", 0, tuple(propertie s));
if not doc:
print "Failed to open '%s'" % file
return
# Save File
properties=[]
p=PropertyValue ()
p.Name="Overwri te"
p.Value=True
properties.appe nd(p)
p=PropertyValue ()
p.Name="FilterN ame"
p.Value=export_ format
properties.appe nd(p)
p=PropertyValue ()
p.Name="Hidden"
p.Value=True
basename=os.pat h.basename(file )
idx=basename.rf ind(".")
assert(idx!=-1)
basename=basena me[:idx]
url_save="%s/%s.%s" % (out_url, basename, export_extensio n)
try:
doc.storeToURL(
url_save, tuple(propertie s))
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.arg v[2]
out_url="file://%s" % os.path.abspath (out_dir)
print out_url
# Init: Connect to running soffice process
context = uno.getComponen tContext()
resolver=contex t.ServiceManage r.createInstanc eWithContext(
"com.sun.star.b ridge.UnoUrlRes olver", context)
try:
ctx = resolver.resolv e(
"uno:socket,hos t=localhost,por t=2002;urp;Star Office.Componen tContext")
except:
print "Could not connect to running openoffice."
usage()
sys.exit()
smgr=ctx.Servic eManager
desktop = smgr.createInst anceWithContext ("com.sun.star. frame.Desktop", ctx)
files=os.listdi r(in_dir)
files.sort()
for file in files:
print "Processing %s" % file
file=os.path.jo in(in_dir, file)
do_file(file, desktop, out_url)
if __name__=="__ma in__":
main()
Comment