[CALC] Automatically save columns as text files

there is a way to automatically save the columns of a sheet as different text files?


Here an example



When you say “automatic”, what is exactly?

An file with exactly you want, will help to help you.

With a button or a menu item, something like that.

With button not is automatic, but, if this it’s ok for you… only I need an example.

I’ve attached a simple example of file.

I see your file, but, Only in your mind are how to needed export this information. A text file can be many different things.

Ok, now there is an image (I cannot attach txt files!) with that I would like :slight_smile:

First version in Basic. Caution: If you have a few data, this code work fine, with many (5000+) rows, maybe run slow then we need refactory code.

Sub export_to_txt()
	path_target = "/home/mau/"
	doc = ThisComponent
	sheet = doc.CurrentController.ActiveSheet
	cell = sheet.getCellRangeByname("A1")
	cursor = sheet.createCursorByRange(cell)
	data = cursor.DataArray
	For c = 0 To UBound(data(0))
		ff = FreeFile()
		path = path_target & data(0)(c) & ".txt"
		Open path For Output As ff
			Print #ff, data(0)(c)
			For r = 1 To UBound(data)
				Print #ff, data(r)(c)
			Next r
		Close #ff
	Next c
End Sub

Version Python, more simple:

import uno

def main():

    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.CurrentController.ActiveSheet
    cursor = sheet.createCursorByRange(sheet['A1'])
    data = cursor.DataArray

    for c in range(len(data[0])):
        values = [r[c] for r in data]
        path = '/home/mau/{}.txt'.format(values[0])
        with open(path, 'w') as f:


Version using my library easymacro

import easymacro as app

def main():

    data = app.get_cell('A1').current_region.data
    for c in range(len(data[0])):
        values = [r[c] for r in data]
        path = '/home/mau/{}.txt'.format(values[0])
        app.save_file(path, data='\n'.join(values))

1 Like

Thanks a lot!!

Python & LibreOffice? I’ve discovered a new wonderful world :slight_smile:

I’ve used the first python macro, a couple of adjustments and it works like a charm!

I quickly tried the second version, but I couldn’t get it to work (probably I haven’t understood where to put easymacro.py :frowning: ).

I think there is also an error here in the doc: https://gitlab.com/mauriciobaeza/zaz/-/wikis/easymacro.py

I think a final colon ‘:’ is missing here:

def test_easymacro()

Thanks, I fixed it.

hey mauricio, i copy-pasted it and it shows an I/O error in line 16. can you please advice?