Use the LibreOffice app in Python (2) Manipulate calc (from macros and externals)

First, let's operate calc.

The operation is simple, just put a number in cell A1. I'm trying with ** windows **.

As a preparation, first, launch a new calc.

macro

Please put the python script that wrote the following in the place of your own macro.

def a1():
  doc = XSCRIPTCONTEXT.getDocument()
  sheet = doc.getSheets().getByName('Sheet1')
  A1 = sheet.getCellRangeByName('A1')
  A1.Value = 100

Execute Tools-> Macro-> Manage Macros-> python-> Applicable Script-> a1. To A1 Contains 100.

** Operate from the outside **

Since it is difficult to debug with macros, we will make it possible to operate using python scripts from the outside. For now, windows can effectively only use python, which comes with LibreOffice. The situation seems to be the same on Mac and Ubuntu. In other words, it's quite inconvenient ** because you can't add modules freely **.

** (Preparation) ** --python uses LibreOffice attachment (does not conflict with other pythons) --Pass the path through LibreOffice's python. --Start libreoffice ** in a state where it can be operated (explained next) **

(Start-up) Start with a batch command (please name it freely) with options as shown below. Note that there are two lines. The DOS window stays open. It's better to make it a service, but for the time being, this is the way to go. Once launched, ** click once on the sheet. ** The cause is unknown, but it doesn't work unless you click it once.

"C:\Program Files (x86)\LibreOffice 5\program\soffice.exe" ^
--calc --norestore --accept=socket,host=localhost,port=8100;urp

The following does the same thing as a macro. Since it comes with libreoffice, uno is imported You should be able to. It is assumed that a new calc is open. access the current document The following is the same content as the macro. Before that, there are still some parts that I don't understand. Execute with the python script name (free name).

# -*- coding: utf-8 -*-
import uno
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
    "com.sun.star.bridge.UnoUrlResolver", localContext)
#connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

# access the current  document ( calc )
doc = desktop.getCurrentComponent()
sheet = doc.getSheets().getByName('Sheet1')
A1 = sheet.getCellRangeByName('A1')
A1.Value = 999

Development is better than macros alone, as macros and the outside world can now do the same thing. Even though it is external, it is inconvenient because pip and ipython cannot be used because you have to use the libreoffice attachment (at the moment).

Recommended Posts

Use the LibreOffice app in Python (2) Manipulate calc (from macros and externals)
Use the LibreOffice app in Python (3) Add library
Using the LibreOffice app in Python (1) Where are the macros?
Manipulate the clipboard in Python and paste the table into Excel
Manipulate files and folders in Python
Use the Flickr API from Python
How to use is and == in Python
Fill the string with zeros in python and count some characters from the string
Connect to postgreSQL from Python and use stored procedures in a loop.
Deploy and use the prediction model created in Python on SQL Server
How to use the C library in Python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
About the difference between "==" and "is" in python
Load and execute command from yml in python
Use PIL in Python to extract only the data you want from Exif
Use Python in your environment from Win Automation
Operate the schedule app using python from iphone
Use the nghttp2 Python module from Homebrew from pyenv's Python
Obtain location information (latitude and longitude) from the address. Geocode in Python ~ Geocoder and pydams ~
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
Receive the form in Python and do various things
App development to tweet in Python from Visual Studio 2017
Carefully understand the exponential distribution and draw in Python
Plot and understand the multivariate normal distribution in Python
I wanted to use the Python library from MATLAB
Use the CASA Toolkit in your own Python environment
Find the part that is 575 from Wikipedia in Python
Carefully understand the Poisson distribution and draw in Python
% And str.format () in Python. Which one do you use?
Find the Hermitian matrix and its eigenvalues in Python
Let's use the open data of "Mamebus" in Python
How to use the model learned in Lobe in Python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use MySQL from Python
Use MySQL from Python
Manipulate riak from python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
2015-12-26 python2> datetime> Implementation to take the difference in seconds from two ISO format datetime strings> Use .seconds ()
[Python] When you want to import and use your own package in the upper directory
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
How to use the __call__ method in a Python class
Get the MIME type in Python and determine the file format
Sort and output the elements in the list as elements and multiples in Python.
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Get the value while specifying the default value from dict in Python
[Python] I installed the game from pip and played it
I tried programming the chi-square test in Python and Java.
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Get the current date and time in Python, considering the time difference
[Tips] Problems and solutions in the development of python + kivy
What beginners learned from the basics of variables in python
Use let expression in Python
Use Measurement Protocol in Python
Working with LibreOffice in Python
Use callback function in Python