python Libre / OpenOffice绘制脚本以批量导出数字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Libre / OpenOffice绘制脚本以批量导出数字相关的知识,希望对你有一定的参考价值。
# 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:])
log = open('%s/exporteps.log' % doc_dir_path, 'a')
global log
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,
以上是关于python Libre / OpenOffice绘制脚本以批量导出数字的主要内容,如果未能解决你的问题,请参考以下文章
使用 Java 访问 Libre,OpenOffice Writer 中的 com.sun.star.text.textfield.Annotation
如何在 Libre/Open Office 中使用 pyUNO 库检查段落调整?
用于 Libreoffice-calc 和 Openoffice-calc 的 Python“Hello World”[关闭]