Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import logging
  3. logging.basicConfig(level=logging.DEBUG)
  4.  
  5. import math
  6.  
  7. import uno
  8. import unohelper
  9.  
  10. from com.sun.star.frame import FrameSearchFlag
  11. from com.sun.star.beans import PropertyValue
  12.  
  13. def log_properties(obj):
  14.     for prop in obj.getPropertySetInfo().getProperties():
  15.         value = obj.getPropertyValue(prop.Name)
  16.         logging.debug("   %s : %s = %s" % (prop.Name, prop.Type, value))
  17.  
  18. def is_field_str(obj):
  19.     return math.isclose(obj.getPropertyValue("Value"), 0.0) and not obj.getPropertyValue("Content").isdigit()
  20.  
  21. localContext = uno.getComponentContext()
  22. resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
  23. context = resolver.resolve("uno:socket,host=localhost,port=8989;urp;StarOffice.ComponentContext")
  24. serviceManager = context.ServiceManager
  25. desktop = serviceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
  26.  
  27. #model = desktop.loadComponentFromURL("private:factory/swriter", "_blank", FrameSearchFlag.SELF, []);
  28. model = desktop.loadComponentFromURL("file:///tmp/template-test2.odt", "_blank", FrameSearchFlag.SELF, []);
  29.  
  30. textFields = model.getTextFields()
  31.  
  32. textFieldMasters = model.getTextFieldMasters()
  33. logging.debug("Get text field masters")
  34.  
  35. builtinExpr = ["com.sun.star.text.fieldmaster.SetExpression.%s" % n for n in ["Illustration", "Table", "Text", "Drawing", "Figure"]]
  36.  
  37. dataStr = {
  38.     "someUserStr": "abcd",
  39.     "someExprStr": "abc"
  40. }
  41.  
  42. dataNum = {
  43.     "someUser": "123458.67",
  44.     "someExpr": "3,65"
  45. }
  46.  
  47. for textFieldMasterName in textFieldMasters.getElementNames():
  48.     logging.debug("textFieldMasterName = %s" % textFieldMasterName)
  49.     if textFieldMasterName not in builtinExpr and (\
  50.     textFieldMasterName.startswith("com.sun.star.text.fieldmaster.User") or \
  51.     textFieldMasterName.startswith("com.sun.star.text.fieldmaster.SetExpression")):
  52.         textFieldMaster = textFieldMasters.getByName(textFieldMasterName)
  53.         logging.debug("Got text field master %s" % textFieldMasterName)
  54.         name = textFieldMaster.getPropertyValue("Name")
  55.         logging.debug("Text field master %s properties:" % name)
  56.         log_properties(textFieldMaster)
  57.        
  58.         if textFieldMasterName.startswith("com.sun.star.text.fieldmaster.User"):
  59.             if is_field_str(textFieldMaster):
  60.                 newContent = dataStr[name]
  61.                 textFieldMaster.setPropertyValue("Content", newContent)
  62.             else:
  63.                 newValue = dataNum[name]
  64.                 textFieldMaster.setPropertyValue("Content", newValue)
  65.             logging.debug("Set value to text field master %s" % textFieldMasterName)
  66.         else:
  67.             for textField in textFieldMaster.getPropertyValue("DependentTextFields"):
  68.                 if is_field_str(textField):
  69.                     newContent = dataStr[name]
  70.                     textField.setPropertyValue("Content", newContent)
  71.                 else:
  72.                     newValue = dataNum[name]
  73.                     textField.setPropertyValue("Content", newValue)
  74.                 logging.debug("Set value to dependent text field %s" % textFieldMasterName)
  75.                 logging.debug("Text field of %s properties:" % name)
  76.                 log_properties(textField)
  77.  
  78. textFields.refresh()
  79.  
  80. model.storeToURL("file:///tmp/output/test1.pdf", [PropertyValue("FilterName", -1, "writer_pdf_Export", 0)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement