-
Notifications
You must be signed in to change notification settings - Fork 476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with Placeholder Replacement in Runs: Text Gets Split, Leading to Formatting Issues #530
Comments
Update: Possible causes: Encoding issue: Some Run.Text() values may not be in UTF-8, or unioffice might handle character encoding inconsistently. func (mbafc *MBAFCTemplate) FillTemplate() {
} // ✅ 新增一个 |
Hi All, I previously read an article mentioning that when using unioffice for {{var}} template placeholder replacement, issues arise where the string gets split into multiple segments, causing mismatches. This has led some developers to reluctantly switch to other Office libraries. Replacing placeholders using raw XML is also cumbersome. After continuous attempts, I am bringing you one of my personal solutions to this issue. It allows placeholder replacement while preserving the original content and formatting. In FillTemplate, placeholders are identified in segments, recording runs and runIndex. Then, the updateRunsV2 method is used to replace content within EG_RunInnerContent, ensuring that both formatting and content are retained. `
}
go version: v1.24.0 Hope this helps you all! |
Description:
Hello Unioffice Team,
I have purchased a Unioffice license and I'm developing an automated report generation system based on placeholders ({{variable}}). However, I'm encountering an issue where Unioffice splits text into multiple runs, causing placeholders to be split into separate pieces and making it difficult to correctly replace them.
If I try to concatenate all runs into a single string, perform the replacements, and then write them back, it leads to formatting loss or broken text.
Reproduction Steps:
1.Create a Word template (template.docx) with placeholders like {{application}}.
2.Open the document with Unioffice and read the paragraph runs.
3.Observe that {{application}} might be split into multiple runs ({{app, lic, ation}}).
4.Attempt to replace the placeholder by merging runs, modifying the text, and writing it back.
5.The formatting is lost, and sometimes the text becomes corrupted.
Current Code (Go)
func (mbafc *MBAFCTemplate) FillTemplate() {
for _, para := range mbafc.template.Paragraphs() {
runs := para.Runs()
if len(runs) == 0 {
continue
}
}
Problem:
Unioffice splits text into multiple runs, which makes placeholder replacement difficult.
Merging runs to perform replacements leads to formatting loss and potential text corruption.
Expected Behavior (Similar to Python’s docxtpl)
In Python, docxtpl allows me to replace placeholders without breaking formatting:
from docxtpl import DocxTemplate
doc = DocxTemplate("template.docx")
context = {'application': 'My Application', 'version': 'V1.0'}
doc.render(context)
doc.save("output.docx")
Question:
How can I achieve similar behavior in Unioffice?
Is there a way to replace text inside runs without losing formatting, or prevent Unioffice from splitting text into multiple runs in the first place?
Screenshots and Output Files



Thank you! 🚀
The text was updated successfully, but these errors were encountered: