Skip to main content

This project is no longer maintained!

Sorry...

PyLOcalc

forthebadge forthebadge forthebadge

Python interface for manipulating LibreOffice Calc spreadsheets

DISCLAIMER: This is not production software! Backup your document before trying it!

About

LibreOffice/OpenOffice has API for many languages including Python, thanks to the Universal Network Objects (UNO).

But its API is all but Pythonic!

I took inspiration from this article and created simple wrapper around this API.

PyLOcalc also automatically opens a headless LibreOffice Calc document with basic read, write, and save functionality. Therefore, it can be used as a library for other scripts that manipulate spreadsheets.

Requirements

  1. python>=3.8

  2. You have to have LibreOffice with "python support" installed. In Ubuntu it's (apparently) sudo apt install libreoffice-script-provider-python, in Arch Linux I didn't have to do anything.

    You can try running python -m uno and it should not produce any output or error.

  3. Macro security - Medium. Follow this article.

Installation

pip install pylocalc

Basic usage

import pylocalc

doc = pylocalc.Document('path/to/calc/spreadsheet.ods')
# You have to connect first
doc.connect()

# Get the sheet by index
sheet = doc[2]
# Or by name
sheet = doc[doc.sheet_names[1]]

# Get the cell by index
cell = sheet[10, 14]
# Or by "name"
cell = sheet['B12']

# Read and set cell value
print(cell.value)
> 'Some value'

cell.value = 12.2
print(cell.value)
> '12.2'

cell.value = 'Other value'
print(cell.value)
> 'Other value'

# Don't forget to save and close the document!
doc.save()
doc.close()

Append rows and columns

PyLOcalc can append row and column values to the first available row or column. It looks at the cell at the offset (default 0) and if the cell is empty it adds values there.

import decimal
import pylocalc

doc = pylocalc.Document('path/to/calc/spreadsheet.ods')
doc.connect()
sheet = doc['Totals']

sheet.append_row(('2021-01-01', 123, 12.3, decimal.Decimal("0.111"), 'Yaaay'), offset=1)

sheet.append_column(('New column header'))

doc.save()
doc.close()

Context manager

PyLOcalc Document can be used as context manager that automatically connects and closes the document. If no error is raised in the context block it also saves the document.

import pylocalc
with pylocalc.Document('path/to/calc/spreadsheet.ods') as doc:
    doc[0][1,10].value = 'I ❤️ context managers'

Release files for pylocalc 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pylocalc 0.5.2
File Size Uploaded
pylocalc-0.5.2.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pylocalc 0.5.2
File Interpreter ABI Platform
pylocalc-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / pylocalc-0.5.2.tar.gz

Download URL pylocalc-0.5.2.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2aa17e93431c7e9dc9886938a940959bb659c75c4c1c0b409066195bfbb0c2c3
BLAKE2b-256 checksum
How to use checksums
a261a9c015f0db71dc55c850686fe4b17851e58b50e9f5a64191451466f46239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0

Release files / pylocalc-0.5.2-py3-none-any.whl

Download URL pylocalc-0.5.2-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f2fcdf7de8faf7897ec42b25971bc3eb190f0a4f9f3bd8dcd71b2b3808924bd3
BLAKE2b-256 checksum
How to use checksums
3b49ac85ed46d4e0a7d1ecc776491561f76f1272b6ff026ea4c648a6250bea62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page