Update lines in ods file using python

Asked

Viewed 100 times

-3

I am looking for a way to update some lines of an existing pre file in ods format using the python language. I managed to create a new file using the code block below, but the need would be to update a file that already exists.

import pyexcel_ods3 as pods

def lerPlanilha(planilha):
    data = pods.get_data(planilha)
    return data

parcela = lerPlanilha("PlanilhaBase.ods")

pods.save_data("saida.ods",parcela)`

1 answer

1


The method get_data() returns an instance of a OrderedDict, where your keys represent the spreadsheet and the value of each key is a list of lists, representing the two-dimensional matrix formed by the cells of each spreadsheet.

For example, assuming your input sheet is something like:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Basic code:

from pyexcel_ods3 import get_data
planilha = get_data("entrada.ods")
print(planilha)

Exit:

OrderedDict([('Sheet1', [['foo', 'bar', 'baz'], ['aeiou', 'kwy', 'blah']]), ('Sheet2', [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])])

The instance of that OrderedDict can be manipulated/modified at will and then saved again in file, see only:

from pyexcel_ods3 import get_data, save_data

# Carrega planilha
planilha = get_data("entrada.ods")

# Altera aleatoriamente o
# texto das celulas
planilha['Sheet1'][0][1] = 'bingo!'
planilha['Sheet1'][1][2] = 'kabum!'
planilha['Sheet1'][1][0] = 'shazam!'

# Multiplica por 2 todos os
# inteiros em todas a celulas
for linha in range(3):
    for coluna in range(4):
        n = planilha['Sheet2'][linha][coluna]
        planilha['Sheet2'][linha][coluna] = n * 2

# Grava planilha modificada
save_data("saida.ods", planilha)

Exit:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • Thanks for the tip Lacobus, in the previous morning I had managed to resolve using ezodf library, so I could percber the procedure is similar to what you indicated in the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.