Last active
November 17, 2016 19:05
-
-
Save basilfx/e08eb57816793b466b285442cf599789 to your computer and use it in GitHub Desktop.
PyOO/Uno: export chart to PNG.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyoo | |
import uno | |
def export_image(context, element, path): | |
""" | |
Export the given element to an image. | |
""" | |
manager = context.getServiceManager() | |
# Choose drawings which you want to export. | |
collection = manager.createInstanceWithContext( | |
"com.sun.star.drawing.ShapeCollection", context) | |
collection.add(element) | |
# Prepare the export arguments. | |
url = uno.systemPathToFileUrl(os.path.abspath(path)) | |
args = [ | |
PropertyValue("MediaType", 0, "image/png", DIRECT_VALUE), | |
PropertyValue("URL", 0, url, DIRECT_VALUE) | |
] | |
# Start exporting. | |
graphic_filter = manager.createInstanceWithContext( | |
"com.sun.star.drawing.GraphicExportFilter", context) | |
graphic_filter.setSourceDocument(collection) | |
return graphic_filter.filter(tuple(args)) | |
# Usage: | |
desktop = pyoo.Desktop("localhost", 2002). | |
sheet = desktop.open_spreadsheet("/tmp/spreadsheet.xlsx").sheets[0] | |
chart = sheet.charts[0] | |
element = chart._target.getEmbeddedObject().getDrawPage().getByIndex(0) | |
export_image(desktop.remote_context, element, "/tmp/file.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment