tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / Spreadsheet / ChartTypeChange.py
blob74b8f42a0f944f5b6fe3bbdbb6c6e69f04373a92
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import sys
11 import time
12 import traceback
13 from typing import Union
15 import officehelper
16 from com.sun.star.awt import Rectangle
17 from com.sun.star.lang import IndexOutOfBoundsException
20 def main():
21 try:
22 remote_context = officehelper.bootstrap()
23 srv_mgr = remote_context.getServiceManager()
24 desktop = srv_mgr.createInstanceWithContext(
25 "com.sun.star.frame.Desktop", remote_context
27 doc = desktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, tuple())
28 sheet = doc.Sheets[0]
30 cell_values = (
31 ("", "Jan", "Feb", "Mar", "Apr", "Mai"),
32 ("Profit", 12.3, 43.2, 5.1, 76, 56.8),
33 ("Rival in business", 12.2, 12.6, 17.7, 20.4, 100),
36 # Write the data into spreadsheet.
37 for row, row_data in enumerate(cell_values):
38 for column, cell_value in enumerate(row_data):
39 insert_into_cell(column, row, cell_value, sheet)
41 # Create a rectangle, which holds the size of the chart.
42 rect = Rectangle()
43 rect.X, rect.Y, rect.Width, rect.Height = 500, 3000, 25000, 11000
45 # Create the Unicode of the character for the column name.
46 char_rect = chr(65 + len(cell_values[0]) - 1)
47 # Get the cell range of the written values.
48 chart_cell_range = sheet[f"A1:{char_rect}{len(cell_values)}"]
49 # Create a table chart with all written values
50 chart_data_source = [chart_cell_range.RangeAddress]
52 sheet.Charts.addNewByName("Example", rect, chart_data_source, True, True)
54 # Get the newly created chart
55 table_chart = sheet.Charts["Example"]
57 # Change chart types one by one
58 chart_types = (
59 "com.sun.star.chart.LineDiagram",
60 "com.sun.star.chart.BarDiagram",
61 "com.sun.star.chart.PieDiagram",
62 "com.sun.star.chart.NetDiagram",
63 "com.sun.star.chart.XYDiagram",
64 "com.sun.star.chart.StockDiagram",
65 "com.sun.star.chart.AreaDiagram",
67 total = len(chart_types)
69 for i, type_name in enumerate(chart_types, start=1):
70 time.sleep(3)
71 print("Change chart type to:", f"[{i}/{total}]", type_name)
72 try:
73 chart_doc = table_chart.EmbeddedObject
74 chart_doc.Title.String = f"Chart Type: {type_name}"
75 diagram = chart_doc.createInstance(type_name)
76 diagram.Dim3D = False
77 chart_doc.Diagram = diagram
78 except Exception as e:
79 print(f"Fail to change chart type to {type_name}: {e}", file=sys.stderr)
80 traceback.print_exc()
81 except Exception as e:
82 print(f"Fail to change chart type: {e}", file=sys.stderr)
83 traceback.print_exc()
86 def insert_into_cell(column: int, row: int, value: Union[str, float], sheet):
87 try:
88 cell = sheet[row, column]
89 except IndexOutOfBoundsException:
90 print("Could not get Cell", file=sys.stderr)
91 traceback.print_exc()
92 else:
93 if isinstance(value, str):
94 cell.String = value
95 else:
96 cell.Value = value
99 if __name__ == "__main__":
100 main()
102 # vim: set shiftwidth=4 softtabstop=4 expandtab: