[Solved] autocorrect-to-csv.py doesn't work on Windows

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

[Solved] autocorrect-to-csv.py doesn't work on Windows

Post by Tommy »

hi, I'm a LibreOffice user.

I've found an interesting python macro on this website:
http://www.linuxquestions.org/questions ... sv-867742/

the code is:

Code: Select all

#-*- coding: utf-8 -*-
import os, sys, zipfile, xml.dom.minidom
##########################################################
# Script to export LibreOffice Auto Correct Entries
# into a flat file (e.g. to reuse some of them with autokey)
##########################################################
ACEfile='.libreoffice/3/user/autocorr/acor_de-DE.dat'    # This is a ZIP where LibreOffice stores its auto correct entries
ifname='DocumentList.xml'                                # Name of the file inside the ZIP archive that contains auto correct entries
ofname='AutoCorrectEntries.csv'                          # any desired output file name for the export
tagname= 'block-list:block'                              # (as in DocumentList.xml)
schema=['block-list:abbreviated-name','block-list:name'] # (as in DocumentList.xml)
default_encoding='UTF-8'                                 # (as in DocumentList.xml)
ofdelimiter=";"                                          # any desired delimiter for export
##########################################################

of = open(ofname,"w")
oACE = zipfile.ZipFile(os.path.join(os.path.expanduser("~"), ACEfile))
zif = oACE.open(ifname, "r") # access as read-only ZipExtFile object
doctree = xml.dom.minidom.parse(zif)  # Parse the input file as DOM (document object model, xml-tree) into memory
if doctree.encoding:
    encoding = doctree.encoding
else:
	encoding = default_encoding
for elem in doctree.getElementsByTagName(tagname):
	acEntry=[]
	for fieldname in schema:
		acEntry.append(elem.getAttribute(fieldname))
	of.write(ofdelimiter.join(acEntry).encode(encoding)+"\n")
of.close()     # Close output file
doctree.unlink # and deallocate DOM object

I copied the code in an empty .txt file and renamed as: autocorrect-to-csv.py

then I placed the .py file inside my LibO 3.6.3 user profile under this path:
\User\LibreOffice 3\user\Scripts\python

then I click Tools/Macro/Run Macro/Personal Macros/autocorrect-to-csv.py
but nothing happens... the "run" button stay greyed out.

is the code wrong (it was created by a Linux user, while I'm on Windows... maybe it has to be adapted in some way..) or did I do some mistake during the importing of that .py script?
Last edited by Hagar Delest on Sat Nov 10, 2012 6:44 pm, edited 1 time in total.
Reason: tagged solved
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: autocorrect-to-csv.py doesn't work on Windows

Post by karolus »

Hallo

All the Code is organized on Scriptlevel, from LO/Oo you 'call' only Functions ( which are organized in Function-blocks ):

Code: Select all

def somefunction():
    do something
The Script seems to be independent from every running LO/OO-instance, so maybe it works if you try from Commandline:

>> python.exe autocorrect-to-csv.py

Karolus
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: autocorrect-to-csv.py doesn't work on Windows

Post by Tommy »

thanks Karolus.
according to what you suggested I installed python 2.7.3 on my Vista 64bit PC.

I was able to make the .py script work with the command line.

I'm just curious why I cannot assign the .py extension files to be opened by python.exe itself,
instead of using the command line each time.

if I double click on a .py file a black python window appears with a flickering cursor but nothing happens.
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] autocorrect-to-csv.py doesn't work on Windows

Post by Tommy »

for the record here's a modified version of the script to make it work on Windows using X-LibreOffice, giving the exact path of the autocorrect file (you should modify according to the location if has on your PC)

Code: Select all

#-*- coding: utf-8 -*-
import os, sys, zipfile, xml.dom.minidom
##########################################################
# Script to export LibreOffice Auto Correct Entries
# into a flat file (e.g. to reuse some of them with autokey)
##########################################################
ACEfile=r'C:\Program Files\OpenOffice\User\LibreOffice 3\user\autocorr\acor_it-IT.dat'   # This is a ZIP where LibreOffice stores its auto correct entries
ifname='DocumentList.xml'                                # Name of the file inside the ZIP archive that contains auto correct entries
ofname='AutoCorrectEntries.csv'                          # any desired output file name for the export
tagname= 'block-list:block'                              # (as in DocumentList.xml)
schema=['block-list:abbreviated-name','block-list:name'] # (as in DocumentList.xml)
default_encoding='UTF-8'                                 # (as in DocumentList.xml)
ofdelimiter=";"                                          # any desired delimiter for export
##########################################################
of = open(ofname,"w")
oACE = zipfile.ZipFile(ACEfile)
zif = oACE.open(ifname, "r") # access as read-only ZipExtFile object
doctree = xml.dom.minidom.parse(zif)  # Parse the input file as DOM (document object model, xml-tree) into memory
if doctree.encoding:
    encoding = doctree.encoding
else:
   encoding = default_encoding
for elem in doctree.getElementsByTagName(tagname):
   acEntry=[]
   for fieldname in schema:
      acEntry.append(elem.getAttribute(fieldname))
   of.write(ofdelimiter.join(acEntry).encode(encoding)+"\n")
of.close()     # Close output file
doctree.unlink # and deallocate DOM object 
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
Post Reply