[CALC] Automatically save columns as text files

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

Willy

Here an example

columns.ods

text_files.png

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.
Thanks

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)
	cursor.collapseToCurrentRegion()
	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'])
    cursor.collapseToCurrentRegion()
    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:
            f.write('\n'.join(values))

    return

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))

    return
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?