I want to substitute variables into an existing document

Hi, I have a document template, I want to open it with Python Docbuilder and replace variables. Is it possible to do this with Docbuilder?

Hello @yt-0r

It depends on how your variables are defined in a document. Can you share an example?

In general, you can utilize SearchAndReplace or ReplaceTextSmart depending on the scenario with line arguments in the script to perform replacement. Please find documentation on how to use line arguments here:

Considering that these variables are plain text, you can use SearchAndReplace() method for that goal. To make the process easier, you can also use command line arguments so that you won’t need to specify values to substitute inside your .docbuilder script.

You have uploaded documentation regarding JS. Is it possible to do this with python docbuilder?

У меня получился такой код, но это не работает
import sys

sys.path.append(“C:/Program Files/ONLYOFFICE/DocumentBuilder”)
import docbuilder

builder = docbuilder.CDocBuilder()
input_file = “123.docx”
output_file = “1234.docx”

try:
builder.OpenFile(input_file, ‘docx’)

context = builder.GetContext()
api = context.GetGlobal()["Api"]

replace_params = {
    "searchString": "{{var}}",
    "replaceString": "world",
}

api.Call("SearchAndReplace", replace_params)

builder.SaveFile("docx", output_file)

except Exception as e:
print(f"Ошибка: {e}")
finally:
builder.CloseFile()

The documentation on SearchAndReplace method that I’ve mentioned earlier explicitly says that it must be executed for the ApiDocument class, but in your it is called for Api, which is incorrect. Please try defining it and running the program again.

Something like this:

api = globalObj["Api"]
document = api.Call("GetDocument")
replace_params = {
    "searchString": "{{var}}",
    "replaceString": "world",
}
document.Call("SearchAndReplace", replace_params)

Thank you! I did it! I would also like to know if it is possible to convert docx to pdf in python docbuilder?

I am glad to know that it helped.

To convert the document you can basically open a DOCX document and in the end save it as PDF with SaveFile method.

Thank you for your help, I really faced problems related to python docbuilder and office api. How can I read the contents of a document? I try to read at least something, but I get a python object in response. I want to get the text and its location on the document

sys.path.append('C:/Program Files/ONLYOFFICE/DocumentBuilder')
import docbuilder

builder = docbuilder.CDocBuilder()
input_file = '123.docx'

input_file = os.path.abspath(input_file)

builder.OpenFile(input_file, 'docx')
context = builder.GetContext()
api = context.GetGlobal()["Api"]
document = api.Call("GetDocument")

paragraph = document.Call('GetElement', 0)

element = paragraph.Call("GetElement", 0)
print(element)

Please start a new topic for this query with all details and desired goal as it differs from the original question.

As I can see you have created a separate topic, thank you. Closing this one as solved.