Hallo
So:
Der Basiccode:
Code: Alles auswählen
REM Keep a global reference to the ScriptProvider, since this stuff may be called many times:
Global g_MasterScriptProvider
REM Specify location of Python script, providing cell functions:
Const URL_Main = "vnd.sun.star.script:sheetfunction.py$"
Const URL_Args = "?language=Python&location=user"
Function getMasterScriptProvider()
if NOT isObject(g_MasterScriptProvider) then
oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
g_MasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
endif
getMasterScriptProvider = g_MasterScriptProvider
End Function
Function kontrakt_aus_datei( text )
sURL = URL_Main & "kontrakt_aus_datei" & URL_Args
oMSP = getMasterScriptProvider()
oScript = oMSP.getScript(sURL)
x = oScript.invoke( Array( text),Array(),Array())
kontrakt_aus_datei = x
end function
Function kontrakt_aus_zelltext( text )
sURL = URL_Main & "kontrakt_aus_zelltext" & URL_Args
oMSP = getMasterScriptProvider()
oScript = oMSP.getScript(sURL)
x = oScript.invoke(Array( text ),Array(),Array())
kontrakt_aus_zelltext = x
end Function
Den kopiertst du in eine neues Modul unter →Meine Makros→
Standard...
Pythonsourcecode:
Code: Alles auswählen
# -*- coding: utf-8 -*-
from __future__ import division
import sys
import uno
import re , urllib, urllib2
wrapper = uno.createUnoStruct('com.sun.star.script.ArrayWrapper')
wrapper.IsZeroIndex = False
def kontrakt_aus_datei( surl ):
with open( surl ) as text:
text = text.read()
return kontraktaustext( text )
def kontrakt_aus_zelltext( zelltext ):
return kontraktaustext( zelltext.encode("utf8"))
def kontraktaustext(text):
text = "%s" %(text)
pattern = re.compile(
r".*?Kontrakt Nr\. (\d+)" # Kontraktnr.
".*?Absender: ([\w äüößÄÜÖ]+)" # von ?
".*?nach ([\w äöüßÄÜÖ]+)" # nach ?
".*?([\d.]+) ([\w äöüßÄÜÖ]+?)" # Menge und Produkt
"\s*\(Qualit(?:ä|ä)t (\d+)\)" # Qualität
"\s*Einzelpreis: ([\d,]+)", re.S ) # Preis
liste = pattern.findall( text )
wrapper.Array = tuple( liste )
return wrapper
Da nimmst du am besten das angehängte Zip-archiv und entpackst es in den Ordner ~/
user/Scripts/ deines OOobenutzerverzeichnisses ( den Pfad dorthin findest du unter →Extras→Optionen→OOo→Pfade... )
Danach kannst du in Calc mit der Funktion:
( ! als Matrixfunktion mit
strg+shift+enter bzw. mit der [x]
Matrix-option im Formelassistenten )
deine gewünschten Daten in 7 Zellen nebeneinander ausgeben lassen.
Mit etwas Glück*** kannst mit der anderen Funktion "kontrakt_aus_datei"**** die Daten direkt aus einer Datei auslesen, in dem Fall dann mit dem Pfad zu Datei als Argument.
***Sie braucht richtig viel Zeit bei meinen Tests
****Edit: Jetzt läuft auch die Funktion in normaler Geschwindigkeit
*******edit2: Den Syntaxfehler im Pythonscript berichtigt.
Gruß Karo