Skip to content
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

font color? #16

Open
jnylund opened this issue May 30, 2014 · 3 comments · May be fixed by #26
Open

font color? #16

jnylund opened this issue May 30, 2014 · 3 comments · May be fixed by #26

Comments

@jnylund
Copy link

jnylund commented May 30, 2014

Hi, is there anyway to set the color of the text that is inserted using insert_text_after?

If not, I can try to add it, but can someone give me a hint of where to look or generally what to do? (I don't know anything about docx format).

thanks
Joel

@Steimel
Copy link

Steimel commented Jun 4, 2014

Here's a way to do it using Nokogiri (which is what docx uses under the hood)

require 'docx'
require 'nokogiri'

def insert_colored_text_after(bookmark, text, hex_color)
    color = Nokogiri::XML::Node.new("w:color", bookmark.node)
    color['w:val'] = hex_color

    text_run = bookmark.get_run_after
    text_run.text = "#{text}#{text_run.text}"

    text_run.node.children.each do |child|
        #Find the node for formatting
        if child.name == "rPr"
            child<<color
        end
    end
end

@englertjoseph englertjoseph linked a pull request Nov 14, 2015 that will close this issue
@matthewgani
Copy link

@Steimel Thank you for the solution, really appreciate it!

@eyupatis
Copy link

Thanks @Steimel for your help.

Here is a very similar version of the method, if you already have a paragraph object:

def add_color_to_paragraph(paragraph, color)
  color_node = Nokogiri::XML::Node.new("w:color", paragraph.node.document)
  color_node["w:val"] = color

  paragraph.each_text_run do |text_run|
    text_run.node.children.each do |child|
      # Find the node for formatting
      child << color_node if child.name == "rPr"
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants