Advertisement
Guest User

Untitled

a guest
Jun 19th, 2020
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None
  1. #!/usr/bin/env python3
  2. import logging
  3. logging.basicConfig(level=logging.DEBUG)
  4.  
  5. import uno
  6. import unohelper
  7.  
  8. from com.sun.star.frame import FrameSearchFlag
  9. from com.sun.star.beans import PropertyValue
  10.  
  11. localContext = uno.getComponentContext()
  12. resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
  13. context = resolver.resolve("uno:socket,host=localhost,port=8989;urp;StarOffice.ComponentContext")
  14. serviceManager = context.ServiceManager
  15. desktop = serviceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
  16.  
  17. #model = desktop.loadComponentFromURL("private:factory/swriter", "_blank", FrameSearchFlag.SELF, []);
  18. model = desktop.loadComponentFromURL("file:///tmp/template-test.odt", "_blank", FrameSearchFlag.SELF, []);
  19.  
  20. textFields = model.getTextFields()
  21.  
  22. textFieldMasters = model.getTextFieldMasters()
  23. logging.debug("Get text field masters")
  24.  
  25. builtinExpr = ["com.sun.star.text.fieldmaster.SetExpression.%s" % n for n in ["Illustration", "Table", "Text", "Drawing", "Figure"]]
  26.  
  27. data = {
  28.     "someUser": "12345.67",
  29.     "someExpr": "80.65"
  30. }
  31.  
  32. for textFieldMasterName in textFieldMasters.getElementNames():
  33.     logging.debug("textFieldMasterName = %s" % textFieldMasterName)
  34.     if textFieldMasterName not in builtinExpr and (\
  35.     textFieldMasterName.startswith("com.sun.star.text.fieldmaster.User") or \
  36.     textFieldMasterName.startswith("com.sun.star.text.fieldmaster.SetExpression")):
  37.         textFieldMaster = textFieldMasters.getByName(textFieldMasterName)
  38.         logging.debug("Got text field master %s" % textFieldMasterName)
  39.         name = textFieldMaster.getPropertyValue("Name")
  40.         logging.info("Name = %s" % name)
  41.        
  42.         newContent = data[name]
  43.         if textFieldMasterName.startswith("com.sun.star.text.fieldmaster.User"):
  44.             textFieldMaster.setPropertyValue("Content", newContent)
  45.             logging.debug("Set value to text field master %s" % textFieldMasterName)
  46.         else:
  47.             for textField in textFieldMaster.getPropertyValue("DependentTextFields"):
  48.                 textField.setPropertyValue("Content", newContent)
  49.                 logging.debug("Set value to dependent text field %s" % textFieldMasterName)
  50.  
  51. textFields.refresh()
  52.  
  53. model.storeToURL("file:///tmp/output/test1.pdf", [PropertyValue("FilterName", -1, "writer_pdf_Export", 0)])
Advertisement
RAW Paste Data Copied
Advertisement