# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- # # This file is part of the LibreOffice project. # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. #
import sys import time import traceback
import officehelper from com.sun.star.awt import Rectangle from com.sun.star.lang import IndexOutOfBoundsException
"""
Step 1: get the remote component context from the office
Step 2: open an empty calc document
Step 3: create cell styles
Step 4: get the sheet an insert some data
Step 5: apply the created cell styles
Step 6: insert a 3D Chart """
def main(): # oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooooo # call UNO bootstrap method and get the remote component context form # the a running office (office will be started if necessary) try:
remote_context = officehelper.bootstrap()
print("Connected to a running office ...")
srv_mgr = remote_context.getServiceManager()
desktop = srv_mgr.createInstanceWithContext( "com.sun.star.frame.Desktop", remote_context
) except Exception as e:
print(f"Couldn't get Sheet: {e}")
traceback.print_exc()
sys.exit(1)
# oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooooo # open an empty document. In this case it's a calc document. # For this purpose an instance of com.sun.star.frame.Desktop # is created. The desktop provides the XComponentLoader interface, # which is used to open the document via loadComponentFromURL
print("Opening an empty Calc document")
doc_url = "private:factory/scalc" try:
doc = desktop.loadComponentFromURL(doc_url, "_blank", 0, tuple()) except Exception as e:
print(f"Couldn't get Sheet: {e}")
traceback.print_exc() return
# oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooooo # create cell styles. # For this purpose get the StyleFamiliesSupplier and the family CellStyle. # Create an instance of com.sun.star.style.CellStyle and add it to the family. # Now change some properties
# oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooooo # get the sheet an insert some data. # Get the sheets from the document and then the first from this container. # Now some data can be inserted. For this purpose get a Cell via # getCellByPosition and insert into this cell via setValue() (for floats) # or setFormula() for formulas and Strings. # As a Python example, those calls are made in equivalent Pythonic ways.
print("Getting spreadsheet") try:
sheet = doc.Sheets[0] except Exception as e:
print(f"Couldn't get Sheet: {e}")
traceback.print_exc()
sys.exit(1)
# oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooooo # apply the created cell style. # For this purpose get the PropertySet of the Cell and change the # property CellStyle to the appropriate value.
change_backcolor(1, 0, 13, 0, "My Style", sheet)
change_backcolor(0, 1, 0, 3, "My Style", sheet)
change_backcolor(1, 1, 13, 3, "My Style2", sheet)
# oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooooo # insert a 3D chart. # get the CellRange which holds the data for the chart and its RangeAddress # get the TableChartSupplier from the sheet and then the TableCharts from it. # add a new chart based on the data to the TableCharts. # get the ChartDocument, which provide the Diagram. Change the properties # Dim3D (3 dimension) and String (the title) of the diagram.
# get the diagram and change some of the properties try:
chart = sheet.Charts["Example"] # chart object implements XEmbeddedObjectSupplier interface
diagram = chart.EmbeddedObject.Diagram
print("Change Diagram to 3D")
diagram.Dim3D = True
print("Change the title")
time.sleep(.2)
chart.EmbeddedObject.Title.String = "The new title" except Exception as e:
print(f"Changing Properties failed: {e}", file=sys.stderr)
traceback.print_exc()
print("done")
def insert_into_cell(column: int, row: int, value: str, sheet, flag: str): try:
cell = sheet[row, column] except IndexOutOfBoundsException:
print("Could not get Cell", file=sys.stderr)
traceback.print_exc() else: if flag == "V":
cell.Value = float(value) else:
cell.Formula = value
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.