Skip to content

Text Replacement not working as Expected #147

@manovasanth1227

Description

@manovasanth1227

Describe the bug

Hi Team, I am looking to replace content or text that contains a specific pattern, such as {{sample}}. When I use the existing substitute method in the TextRun class, it appears to not be working.

Upon debugging the issue, I discovered that while reading the DOCX file, the nodes (specifically w:t elements) have been split into multiple tags.
For example, if the word is {{vorname}},

paragraph.each_text_run => returns array of text_run object

For Easy Explanation I will represent array of text_run object as like this: ['{{', 'v', 'orname', '}}'].
Consequently, when attempting to replace the text content using the substitute method, which checks the TextRun objects one by one, it misses the word that exists in the first place due to this splitting.

To Reproduce

Have content being edited multiple times in docx file, It will eventually break into multiple nodes as described above.

example

require 'docx'
path = "/Users/mb/Downloads/sample.docx" 
doc = Docx::Document.new(path)
doc.paragraphs.each_with_index do |paragraph, index|
  puts "paragraph: #{index} text: #{paragraph.text}"
  paragraph.each_text_run do |text_run|
    puts "text: #{text_run.text}" 
    text_run.substitute('{{vorname}}', 'Not Working Fine') 
# it fails to replace the placeholder when only a portion of the placeholder text is present, rather than the complete word.
  end
end

## output for the above code :
paragraph: 0 text: 24 Nov, 2023
text: 24 
text: Nov,
text:  2023
paragraph: 1 text: 
paragraph: 2 text: {{vorname}}
text: {{
text: v
text: orname
text: }}
paragraph: 3 text: 
paragraph: 4 text: {{nachname}}
text: {{
text: n
text: achname
text: }}
paragraph: 5 text: {{Stellentitel}}
text: {{
text: Stellentitel
text: }}
paragraph: 6 text: 
paragraph: 7 text: Subject: Test
text: Subject: 
text: Test
paragraph: 8 text: 
paragraph: 9 text: {{Name}}
text: {{
text: Name}}
paragraph: 10 text: {{asaso}}
text: {{
text: asas
text: o}}
paragraph: 11 text: {{Manovasanth}}
text: {{Manovasanth}}

Sample docx file

sample.docx

Expected behavior

Correctly replace the text placeholder text( {{vorname}}, {{Manovasanth}} ) with the given replacement text

Environment

  • Ruby version: [e.g 2.6.9]
  • docx gem version: [e.g 0.6.2]
  • OS: [e.g. MacOS 13.5.2]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions