Skip to content

Order of paragraphs and tables #118

Open

Description

@Prigin

Problem

I need to get all paragraphs and tables in order they have in docx file. Is there any way I can do this?

Solution

May be just one index for paragraph objects and table objects will be enough.

Activity

  1. satoryu commented on Nov 18, 2021

    @satoryu
    Member

    You mean that at the latest version of this gem Document#paragraphs returns paragraphs in wrong order, right?
    Could you give us a docx file to reproduce this behavior if you have?
    The file would help us to investigate what happens.

    Thanks

  2. Prigin commented on Nov 18, 2021

    @Prigin
    Author

    Not exactly. :) Sorry for not being transparent. Lets say I have a docx that I want to convert to txt:

    image

    I need to know place of each element(paragraphs and tables). How to get the same order of elements they have in DOCX? Or maybe they already have that method(which returns order number from doc). I cant actually find it :(

  3. aunghtain commented on Mar 10, 2022

    @aunghtain

    I was able to do this as followed. I'm using private vars/methods, but if they open up more APIs in the future, we won't have to.

        doc = Docx::Document.open(file)
        doc.instance_variable_get("@doc").xpath('//w:document//w:body').children.each do |c|
          if c.name == 'p' # paragraph
            p = doc.send(:parse_paragraph_from, c)      
          elsif c.name = 'tbl' # table
            t = doc.send(:parse_table_from, c)  
          else # other types?
          end
        end
  4. aunghtain commented on Mar 10, 2022

    @aunghtain

    if u just want text, u don't need to parse them as paragraph/table. u can just get as "c.content"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions