dotfiles/VSCodium/User/History/-2fd23f3b/bzMz.py
2026-06-19 14:29:55 +02:00

29 lines
1.3 KiB
Python

from odfpy import opendocument
from odf.table import Table
def sheet_text(path):
doc = opendocument.load(path)
table = list(doc.spreadsheet.getElementsByType(Table))[0]
rows = list(table.getElementsByType(TableRow))
out = []
for r, row in enumerate(rows):
cells = []
for c, cell in enumerate(row.childNodes):
if not hasattr(cell, 'tagName'):
continue
if cell.tagName not in ('table:table-cell', 'table:covered-table-cell'):
continue
span = int(cell.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:table:1.0', 'number-columns-spanned'), 1))
repeat = int(cell.attributes.get(('urn:oasis:names:tc:opendocument:xmlns:table:1.0', 'number-columns-repeated'), 1))
ps = cell.getElementsByType(opendocument.text.P)
text = ''.join(str(p) for p in ps)
text = text.replace('</text:p>', '').replace('<text:p>', '').replace('<text:p/>', '')
cells.append(f'{cell.tagName}:{c}:{span}x{repeat}={text[:30]}')
out.append(f'row{r+1}: ' + ' | '.join(cells))
return '\n'.join(out)
with open('/tmp/example_rows.txt','w') as f:
f.write(sheet_text('exampleA4004-2603N10.ods'))
with open('/tmp/gen_rows.txt','w') as f:
f.write(sheet_text('A4004-2603N10.ods'))
print('done')