Skip to main content

Python interface for manipulating LibreOffice Calc spreadsheets

Project description

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'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylocalc-0.5.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

pylocalc-0.5.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pylocalc-0.5.2.tar.gz.

File metadata

  • Download URL: pylocalc-0.5.2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0

File hashes

Hashes for pylocalc-0.5.2.tar.gz
Algorithm Hash digest
SHA256 2aa17e93431c7e9dc9886938a940959bb659c75c4c1c0b409066195bfbb0c2c3
MD5 509a572b7ad30f19b03f87b5840e385a
BLAKE2b-256 a261a9c015f0db71dc55c850686fe4b17851e58b50e9f5a64191451466f46239

See more details on using hashes here.

File details

Details for the file pylocalc-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: pylocalc-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.1 Darwin/21.2.0

File hashes

Hashes for pylocalc-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f2fcdf7de8faf7897ec42b25971bc3eb190f0a4f9f3bd8dcd71b2b3808924bd3
MD5 f29197ec20315a5e141420d40fe422a7
BLAKE2b-256 3b49ac85ed46d4e0a7d1ecc776491561f76f1272b6ff026ea4c648a6250bea62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page