Skip to content

Instantly share code, notes, and snippets.

@brechtm
Last active November 21, 2022 23:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brechtm/9258536 to your computer and use it in GitHub Desktop.
Save brechtm/9258536 to your computer and use it in GitHub Desktop.
Libre/OpenOffice Draw script to bulk export figures
# Libre/OpenOffice: Export selected or all figures as EPS
# 2010 Brecht Machiels
#
# Usage:
# - drop script in user scripts folder
# Max OSX: $HOME/Library/Application Support/LibreOffice/4/user/scripts/python/
# (Arch) Linux: $HOME/.config/libreoffice/4/user/Scripts/python/
# other Linux: $HOME/.libreoffice/4/user/Scripts/python/
# Windows: C:\Document and Settings\<username>\Application Data\libreoffice\4\user\Scripts\python
# - start Draw and draw some figures
# - group all shapes in each figure (select shapes and then Modify->Group)
# - name the groups (right-click the group and select "Name...")
# - select the groups you want to export, or none if you want to export them all
# - run the script
# * open Tools->Macros->Organize Macros->Python...
# * select My Macros->exporteps->exportEPS and press "Run"
# * figures will be exported as <name>.eps in the same directory as the odg file
#
# You probably want to bind a keyboard shortcut to exportEPS:
# * open Tools->Customize... Keyboard tab
# * select a shortcut key in the list (Ctrl+Shift+X for example)
# * under Functions>Category select LibreOffice Macros->user->exporteps
# * click "Modify"
#
# You can change some EPS export options below (see "Predefined export options")
# Some webpages that have been useful:
# http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Drawings/Exporting
# might be useful: http://www.oooforum.org/forum/viewtopic.phtml?t=6769
# http://www.oooforum.org/forum/viewtopic.phtml?t=51021
DEBUG = False
if DEBUG:
def debug(line):
log.write(line + '\n')
else:
def debug(line):
pass
def exportEPS():
"""Export each selected shape as <name>.eps"""
import uno
try:
# Python 3
from urllib.request import url2pathname
except ImportError:
# Python 2
from urllib import url2pathname
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
from com.sun.star.awt import Rectangle
from com.sun.star.beans import PropertyValue
from com.sun.star.beans.PropertyState import DIRECT_VALUE
def PropVal(name, value):
return PropertyValue(name, 0, value, DIRECT_VALUE)
document = XSCRIPTCONTEXT.getDocument()
context = XSCRIPTCONTEXT.getComponentContext()
export_filter = context.ServiceManager.createInstanceWithContext(
'com.sun.star.drawing.GraphicExportFilter', context)
msgbox_factory = context.ServiceManager.createInstanceWithContext(
'com.sun.star.awt.Toolkit', context)
#filter_dialog = context.ServiceManager.createInstanceWithContext(
# 'com.sun.star.svtools.SvFilterOptionsDialog', context)
selection_supplier = document.getCurrentController()
selected_shapes = selection_supplier.getSelection()
if selected_shapes:
shapes = [selected_shapes.getByIndex(i)
for i in range(selected_shapes.getCount())]
else:
# export all shapes in document
shapes = []
pages = document.getDrawPages()
for i in range(pages.getCount()):
page = pages.getByIndex(i)
for j in range(page.getCount()):
shape = page.getByIndex(j)
shapes.append(shape)
doc_url = document.getURL()
if not doc_url:
parent = (document.getCurrentController().getFrame()
.getContainerWindow())
buttons = BUTTONS_OK
msgbox = msgbox_factory.createMessageBox(parent, Rectangle(),
"infobox", buttons, 'Notice',
'Please save the document first!')
msgbox.execute()
return
doc_dir = str(doc_url).rsplit('/', 1)[0]
if DEBUG:
doc_dir_path = url2pathname(doc_dir[6:])
global log
log = open('%s/exporteps.log' % doc_dir_path, 'a')
debug('number of shapes: %d' % len(shapes))
# Predefined export options
filter_data = (# PS level: 1 or 2
PropVal('Version', 2),
# Color format: 1=color, 2=grayscale
PropVal('ColorFormat', 1),
# How to export text: 0=glyph outlines, 1=as text
PropVal('TextMode', 0),
# Insert preview image: 0=none, 1=TIFF, 2=EPSI, 3=TIFF+EPSI
PropVal('Preview', 0),
# Bitmap compression: 1=LZW, 2=none
PropVal('CompressionMode', 1))
filter_data_propval = PropVal('FilterData',
uno.Any('[]com.sun.star.beans.PropertyValue',
filter_data))
# Pop up dialog for setting the export options
#filter_dialog.setPropertyValues((PropVal('FilterName',
# 'draw_eps_Export'), ))
#filter_dialog.execute()
#filter_data_propval = filter_dialog.PropertyValues[-1]
for shape in shapes:
if not shape.supportsService('com.sun.star.presentation.Shape'):
continue
name = shape.getName()
if name:
export_url = doc_dir + '/' + name + '.eps'
debug('name = "%s" -> %s' % (name, export_url))
export_filter.setSourceDocument(shape)
props = (PropVal('MediaType', 'image/x-eps'),
PropVal('URL', export_url),
filter_data_propval)
rc = export_filter.filter(props)
debug('rc = %d' % rc)
if DEBUG:
log.close()
# lists the scripts, that shall be visible inside OOo. Can be omited, if
# all functions shall be visible
g_exportedScripts = exportEPS,
@aappddeevv
Copy link

Just what I was looking for. But I had an error:

Message:<class 'SyntaxError'>: name 'log' is assigned to before global declaration(exporteps.py, line 99)

Thoughts?

@brechtm
Copy link
Author

brechtm commented May 11, 2020

Message:<class 'SyntaxError'>: name 'log' is assigned to before global declaration(exporteps.py, line 99)

Try moving global log up one line.

@aappddeevv
Copy link

aappddeevv commented May 11, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment