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

Text not being inserted after bookmark #111

Open
s892307 opened this issue Jul 15, 2021 · 4 comments
Open

Text not being inserted after bookmark #111

s892307 opened this issue Jul 15, 2021 · 4 comments
Labels

Comments

@s892307
Copy link

s892307 commented Jul 15, 2021

Describe the bug

I have the following class to read from a template and create a new docx file:-

require 'docx'

require 'docx' 

class DocX
  def generate_doc
    # Create a Docx::Document object for our existing docx file
    doc = Docx::Document.open("#{Rails.root}/app/lib/docx_templates/sample.docx")

    doc.bookmarks['cypsldshvlxb'].insert_text_after("Hello, world")

    # Iterate over each table
    doc.tables.each do |table|
      last_row = table.rows.last

      # Copy last row and insert a new one before last row
      new_row = last_row.copy
      new_row.insert_before(last_row)

      # Substitute text in each cell of this new row
      new_row.cells.each do |cell|
        cell.paragraphs.each do |paragraph|
          paragraph.each_text_run do |text|
            text.substitute('_placeholder_', 'replacement value')
          end
        end
      end
    end

    doc.save("#{Rails.root}/app/lib/docx_templates/sample_edited.docx")
  end
end

Most of the above is pulled from the README docs for this gem.

The new file is saved without any errors, but the code which inserts text "Hello, world", has no effect. The text substitution in the table works fine, as well as the new row insertion.

I got the bookmark name by just using the below, so I know it exists, but maybe I'm using the wrong name for it?:-

doc.bookmarks.each_pair { |name, object| puts name }

Does anyone know what I could be doing wrong?

To Reproduce

Use class/file above and run the method against a document with a few bookmarks.

Sample docx file

sample.docx

Expected behavior

Expected all text inserts after bookmarks to work.

Environment

  • Ruby version: [2.7.0]
  • docx gem version: [0.6.1]
  • OS: [macOS Big Sur]
@s892307 s892307 added the bug label Jul 15, 2021
@s892307 s892307 changed the title Text not being inserted into document Text not being inserted after bookmark Jul 15, 2021
@satoryu
Copy link
Member

satoryu commented Jul 16, 2021

Thank you for reporting this bug. I just reproduced this problem and the saved docx file does not work.
I need more time to investigate the root cause.

@satoryu
Copy link
Member

satoryu commented Jul 17, 2021

I have analyzed sample.docx, mainly document.xml.
FYI: docx file is a zip file including some XML documents, one of which is document.xml.

Its document.xml looks strange.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
  xmlns:w10="urn:schemas-microsoft-com:office:word"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
  xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main"
  xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
  xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
  xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
  xmlns:lc="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas"
  xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram"
  xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
  xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
  xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
  xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml">
  <w:background w:color="FFFFFF"/>
  <w:body>
    <w:bookmarkStart w:colFirst="0" w:colLast="0" w:name="cypsldshvlxb" w:id="0"/>
    <w:bookmarkEnd w:id="0"/>
    <w:p w:rsidR="00000000" w:rsidDel="00000000" w:rsidP="00000000" w:rsidRDefault="00000000" w:rsidRPr="00000000" w14:paraId="00000001">
      <w:pPr>
        <w:rPr>
          <w:rFonts w:ascii="Arial" w:cs="Arial" w:eastAsia="Arial" w:hAnsi="Arial"/>
          <w:b w:val="1"/>
        </w:rPr>
      </w:pPr>
      <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
        <w:rPr>
          <w:rFonts w:ascii="Arial" w:cs="Arial" w:eastAsia="Arial" w:hAnsi="Arial"/>
          <w:b w:val="1"/>
          <w:rtl w:val="0"/>
        </w:rPr>
        <w:t xml:space="preserve">bookmark_1</w:t>
      </w:r>
    </w:p>

w:bookmarkStart and w:bookmarkEnd tags are not included in paragraph w:p tag.
According to some articles, one of which, these tags should be in w:p tag.
And this gem expects bookmarkStart is in w:p.

I have one question: How is this docx file generated?
If it was generated by popular software, this gem should support it.

@s892307
Copy link
Author

s892307 commented Jul 19, 2021

Hi @satoryu! That's interesting - this was generated using Google Docs.

@satoryu
Copy link
Member

satoryu commented Jul 19, 2021

Ah... I confirmed that Google Docs generates a docx file with the problem I mentioned.
I'll try to fix this issue.

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

No branches or pull requests

2 participants