ruby-docx / docx

a ruby library/gem for interacting with .docx files
MIT License
435 stars 170 forks source link

Edit content from node #79

Closed fercreek closed 3 years ago

fercreek commented 4 years ago

There is some way to replace the content of inspect? If I did something like this

d = Docx::Document.open('example.docx')

d.paragraphs.each do |p|
  p.node.inspect = p.node.inspect.gsub('Old text', 'Next text')
end
misdoro commented 4 years ago

@fercreek check out this one #75 Beware, it was not yet released.

fercreek commented 4 years ago

Yeah! I tried that solution but I have a text with this format. Hi, my name is ${user.full_name}, and when I use each_text_run the text is split like this

$
{
userfull_name
}

Do you have some suggestion? @misdoro

misdoro commented 4 years ago

Except updating your document to use exactly the same formatting for each part of ${userfull_name}, not really. Each text run in a paragraph usually corresponds to a particular styling configuration. You may also try importing/exporting to google docs to try to re-arrange formatting.

brpl20 commented 4 years ago

hey @misdoro I'm using your replacing feature, but there's a problem with similar fields like this : _estado_, "Florida" _estadocivil, "Married"

it will replace like this: "Married" "Marriedcivil_"

I've tried some regex expressions on 'match' but coudn't figure it out

Any idea?

Best regards

misdoro commented 4 years ago

@pellibr you may try changing the order of keys in your replacements hash to be from more specific to less specific, that should be a work-around. Or just replace the more specific terms first, if you are going field by field.

Generally speaking it is not a good idea to use the same symbol as end/begin and space marker. Maybe replace surrounding _ by something else like [] or {}. Or use camelCaseFormat for the template keys.

brpl20 commented 4 years ago

@misdoro That's the best to avoid such problems, thanks