Search

Python Pyoo based script to find the unique elements in a librecalc column

The following is pyoo based python script will be able to list all the unique values in a given column of a librecalc. For example if we have the following file


If we want to find all the unique terms in the column num, just run the following script. Ensure that the path variable is initialized to the full path pointing to the librecalc file.  The script requires pyoo installed, the steps for which are listed here.

Before running the script, make sure to run the following command.





#!/usr/bin/env python3
"""
Tested on python 3.6
"""
import pyoo
path="/media/satish/5229e5b6-0b7b-44ef-a609-5aaf186c6bc4/data/pro_g/python/pyoo/test.ods"
desktop = pyoo.Desktop('localhost', 2002)
doc = desktop.open_spreadsheet(path)
sheet = doc.sheets[0]
column=input('Enter the column header of which unique elements are needed \n')
sheet = doc.sheets[0]
col=0
while (sheet[0,col].value != column):
col=col+1
i=0
addr1=str(sheet[1,col].address)
while(sheet[i,col].value != ''):
i=i+1
last=i
x=sheet[1:last,col].values
unique=list(set(x))
size=len(unique)
print("There are " + str(size) +" unique values")
for i in unique:
print(int(i))
doc.close()


Saved the script as unique_items.py, and execute it. For the above example file shown, we get the following output.

No comments:

Post a Comment