Skip to content

Instantly share code, notes, and snippets.

@JavaScriptDude
Last active December 14, 2020 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JavaScriptDude/d5c9736a3ae41637c5622bdfecc8da22 to your computer and use it in GitHub Desktop.
Save JavaScriptDude/d5c9736a3ae41637c5622bdfecc8da22 to your computer and use it in GitHub Desktop.
Editing xls spreadsheet from Python

Installation

Linux package

sudo apt-get install python3-uno

Add python3 dist-packages to PYTHONPATH:

% dpkg-query -L python3-uno | grep dist

Add Dist Packages to PYTHONPATH in ~/.bashrc

Change this path as required % export PYTHONPATH=/usr/lib/python3/dist-packages

Install pyoo:

% python3 -m pip install pyoo

Using pyoo

Start python uno bridge

Note: all libreoffice executables must be shut down first This will start a headless window that will be used as a bridge for python script to access. This process must be left alive for pyoo to work # soffice --accept="pipe,name=oo_pyuno;urp;" --norestore --nologo --nodefault # --headless

Access from Python - Spreadsheet example

#!/usr/local/bin/python3
import pyoo
desktop = pyoo.Desktop(pipe='oo_pyuno')
wkbk = desktop.open_spreadsheet("<xls_file_name>")
sheet = wkbk.sheets['<sheet_name>']
# Write value 'foo' to cell E5 on <sheet_name>
sheet[4,4].value='foo'
wkbk.save()
wkbk.close()

See: https://pypi.org/project/pyoo/ for more details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment