Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import logging
- logging.basicConfig(level=logging.DEBUG)
- import math
- import uno
- import unohelper
- from com.sun.star.frame import FrameSearchFlag
- from com.sun.star.beans import PropertyValue
- def log_properties(obj):
- for prop in obj.getPropertySetInfo().getProperties():
- value = obj.getPropertyValue(prop.Name)
- logging.debug(" %s : %s = %s" % (prop.Name, prop.Type, value))
- def is_field_str(obj):
- return math.isclose(obj.getPropertyValue("Value"), 0.0) and not obj.getPropertyValue("Content").isdigit()
- localContext = uno.getComponentContext()
- resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
- context = resolver.resolve("uno:socket,host=localhost,port=8989;urp;StarOffice.ComponentContext")
- serviceManager = context.ServiceManager
- desktop = serviceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
- #model = desktop.loadComponentFromURL("private:factory/swriter", "_blank", FrameSearchFlag.SELF, []);
- model = desktop.loadComponentFromURL("file:///tmp/template-test2.odt", "_blank", FrameSearchFlag.SELF, []);
- textFields = model.getTextFields()
- textFieldMasters = model.getTextFieldMasters()
- logging.debug("Get text field masters")
- builtinExpr = ["com.sun.star.text.fieldmaster.SetExpression.%s" % n for n in ["Illustration", "Table", "Text", "Drawing", "Figure"]]
- dataStr = {
- "someUserStr": "abcd",
- "someExprStr": "abc"
- }
- dataNum = {
- "someUser": "123458.67",
- "someExpr": "3,65"
- }
- for textFieldMasterName in textFieldMasters.getElementNames():
- logging.debug("textFieldMasterName = %s" % textFieldMasterName)
- if textFieldMasterName not in builtinExpr and (\
- textFieldMasterName.startswith("com.sun.star.text.fieldmaster.User") or \
- textFieldMasterName.startswith("com.sun.star.text.fieldmaster.SetExpression")):
- textFieldMaster = textFieldMasters.getByName(textFieldMasterName)
- logging.debug("Got text field master %s" % textFieldMasterName)
- name = textFieldMaster.getPropertyValue("Name")
- logging.debug("Text field master %s properties:" % name)
- log_properties(textFieldMaster)
- if textFieldMasterName.startswith("com.sun.star.text.fieldmaster.User"):
- if is_field_str(textFieldMaster):
- newContent = dataStr[name]
- textFieldMaster.setPropertyValue("Content", newContent)
- else:
- newValue = dataNum[name]
- textFieldMaster.setPropertyValue("Content", newValue)
- logging.debug("Set value to text field master %s" % textFieldMasterName)
- else:
- for textField in textFieldMaster.getPropertyValue("DependentTextFields"):
- if is_field_str(textField):
- newContent = dataStr[name]
- textField.setPropertyValue("Content", newContent)
- else:
- newValue = dataNum[name]
- textField.setPropertyValue("Content", newValue)
- logging.debug("Set value to dependent text field %s" % textFieldMasterName)
- logging.debug("Text field of %s properties:" % name)
- log_properties(textField)
- textFields.refresh()
- model.storeToURL("file:///tmp/output/test1.pdf", [PropertyValue("FilterName", -1, "writer_pdf_Export", 0)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement