Hello,
In a loop, I am trying to extract string between 2 markers. After every iteration of the loop, I want to move to the document start using builder.move_to_document_start(). However, it does not seem to work. Here is my code:
def main(raw_file, template_file, final_file, start_markers, end_markers, matched_strings):
doc = aw.Document(raw_file)
for start_marker, end_marker, matched_string in zip(start_markers, end_markers, matched_strings):
# Replace markers with themselves to represent them as single runs.
opt = aw.replacing.FindReplaceOptions()
opt.use_substitutions = True
doc.range.replace_regex(start_marker + "|" + end_marker, "$0", opt)
# Get start and end runs of the range.
start_run = None
end_run = None
for r in doc.get_child_nodes(aw.NodeType.RUN, True):
run = r.as_run()
if run.text.startswith(start_marker):
start_run = run
if run.text.startswith(end_marker):
end_run = run
# Extract content between start and end markers.
nodes = ExtractContentHelper.extract_content(start_run, end_run, False)
# Create a document from the extracted nodes
extracted_doc = ExtractContentHelper.generate_document(doc, nodes)
dst = aw.Document(template_file)
builder = aw.DocumentBuilder(dst)
# Replace placeholder with itself to represent them as single runs.
placeholder = matched_string
dst.range.replace(placeholder, "$0", opt)
# Replace placeholder with the content extracted earlier.
for r in dst.get_child_nodes(aw.NodeType.RUN, True):
run = r.as_run()
if run.text.startswith(placeholder):
builder.move_to(run)
builder.insert_document(extracted_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
# Remove placeholder
run.text = ""
builder.move_to_document_start()
dst.save(final_file)
Any help or advice is appreciated.