import uno
def hello_world():
# Get the component context — the entry point to LibreOffice's internal services
ctx = uno.getComponentContext()
# Use the context to access the ServiceManager, which can create various LibreOffice components
smgr = ctx.ServiceManager
# Create an instance of the desktop interface, which lets us access currently open documents
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
# Get the current document that’s open and active in LibreOffice
model = desktop.getCurrentComponent()
# Access the text part of the document (only works if it's a Writer document)
doc = model.Text
# Insert the string at the start of the document
doc.insertString(doc.getStart(), "Hello, LibreOffice with Python!\n", 0)
rename all text boxes with different names
import uno
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
def rename_text_forms():
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
doc = desktop.getCurrentComponent()
if not hasattr(doc, "DrawPage"):
show_message("Ce document ne contient pas de page de dessin.", doc)
return
draw_page = doc.DrawPage
count = 1
renamedl = []
for i in range(draw_page.getCount()):
shape = draw_page.getByIndex(i)
if hasattr(shape, "Control") and shape.Control:
control = shape.Control
if control.supportsService("com.sun.star.form.component.TextField"):
new_name = f"zdt{count}"
old_name = control.Name
control.Name = new_name
renamedl.append(f"{old_name} → {new_name}")
count += 1
if renamedl:
message = "Renamed : " + "\n".join(renamedl)
else:
message = "No txt zone found"
show_message(message, doc)
def show_message(text, doc):
window = doc.CurrentController.Frame.ContainerWindow
toolkit = window.getToolkit()
box = toolkit.createMessageBox(
window, MESSAGEBOX, BUTTONS_OK, "Result", text
)
box.execute()
change font/fontsize of all txtboxes
import uno
from com.sun.star.awt.MessageBoxType import MESSAGEBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
def show_message(text, doc):
window = doc.CurrentController.Frame.ContainerWindow
toolkit = window.getToolkit()
box = toolkit.createMessageBox(
window, MESSAGEBOX, BUTTONS_OK, "Result", text
)
box.execute()
def change_font_fontsize():
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
doc = desktop.getCurrentComponent()
if not hasattr(doc, "DrawPage"):
show_message("no Drawing page", doc)
return
draw_page = doc.DrawPage
count = 1
modifications = []
for i in range(draw_page.getCount()):
shape = draw_page.getByIndex(i)
if hasattr(shape, "Control") and shape.Control:
control = shape.Control
if control.supportsService("com.sun.star.form.component.TextField"):
old_name = control.Name
new_name = f"zdt{count}"
control.Name = new_name
if hasattr(control, "FontHeight"):
control.FontHeight = 8
if hasattr(control, "FontName"):
control.FontName = "Arial"
modifications.append(f"{old_name} → {new_name} (Arial, size 8)")
count += 1
if modifications:
message = "modified : :\n" + "\n".join(modifications)
else:
message = "No txt forms to change"
show_message(message, doc)