add hacky script to generate einsatzrapporte
This commit is contained in:
parent
3753c1210b
commit
e873c1ff63
52
hack/generate-einsatrapporte.py
Normal file
52
hack/generate-einsatrapporte.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import uno
|
||||||
|
from com.sun.star.beans import PropertyValue
|
||||||
|
from com.sun.star.uno import RuntimeException
|
||||||
|
|
||||||
|
def connect_to_libreoffice(port=2002):
|
||||||
|
localContext = uno.getComponentContext()
|
||||||
|
resolver = localContext.ServiceManager.createInstanceWithContext(
|
||||||
|
"com.sun.star.bridge.UnoUrlResolver", localContext)
|
||||||
|
try:
|
||||||
|
context = resolver.resolve(f"uno:socket,host=localhost,port={port};urp;StarOffice.ComponentContext")
|
||||||
|
return context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
|
||||||
|
except RuntimeException:
|
||||||
|
raise Exception("Make sure LibreOffice is running with a listening port (e.g., soffice --accept=\"socket,port=2002;urp;\" --norestore --nologo --nodefault)")
|
||||||
|
|
||||||
|
def export_to_pdf(doc, file_name):
|
||||||
|
pdf_export_properties = tuple([
|
||||||
|
PropertyValue(Name="FilterName", Value="writer_pdf_Export")
|
||||||
|
])
|
||||||
|
doc.storeToURL(f"file:///{file_name}", pdf_export_properties)
|
||||||
|
|
||||||
|
def replace_text(doc, old_text, new_text):
|
||||||
|
replaceable = doc.createReplaceDescriptor()
|
||||||
|
replaceable.setSearchString(old_text)
|
||||||
|
replaceable.setReplaceString(new_text)
|
||||||
|
|
||||||
|
doc.replaceAll(replaceable)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main(start_counter, num_documents, document_path):
|
||||||
|
desktop = connect_to_libreoffice()
|
||||||
|
|
||||||
|
for i in range(num_documents):
|
||||||
|
counter = start_counter + i
|
||||||
|
load_props = PropertyValue(Name="Hidden", Value=True),
|
||||||
|
doc = desktop.loadComponentFromURL(f"file:///{document_path}", "_blank", 0, load_props)
|
||||||
|
|
||||||
|
replace_text(doc, "counter", str(counter))
|
||||||
|
|
||||||
|
file_name = f"/home/tobru/Documents/Feuerwehr/Stab/Fourier/EinsatzrapporteLeer/Einsatzrapport2024-{counter}.pdf"
|
||||||
|
export_to_pdf(doc, file_name)
|
||||||
|
doc.dispose()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
document_path = "/home/tobru/Documents/Feuerwehr/Stab/Fourier/FW-Einsatzrapport FWU.odt"
|
||||||
|
start_counter = 1
|
||||||
|
num_documents = 100
|
||||||
|
|
||||||
|
# Start libreoffice with:
|
||||||
|
# soffice --accept="socket,port=2002;urp;" --norestore --nologo --nodefault
|
||||||
|
main(start_counter, num_documents, document_path)
|
||||||
|
|
Loading…
Reference in a new issue