Ashish Aggarwal
Ashish Aggarwal

Reputation: 3026

Identify table ID in microsoft word

There are lots of table in my word document. I want to uniquely identify those table but microsoft offfice doesn't provide any unique identifier(ID) to them. So is there any way to identify microsoft word table uniquely?

Problem:
User provides me word file with tables. I have to convert them into images. If the user provides me the same file but the content of the table has been updated then I have to update that image. All delete and again generate all image is not worked in my case because I can't change the name of image I first assign to it.

What I tried.

  1. Generate xml of word doc and look there is any id or unique identifier. But no such thing exists.
  2. Look at table properties where their is only one field alt Text but still not relible because user can change it.

Upvotes: 0

Views: 4109

Answers (1)

edi9999
edi9999

Reputation: 20554

This is how a table looks like in XML (3*3):

<w:tbl>
            <w:tblPr>
                <w:tblStyle w:val="Grilledutableau"/>
                <w:tblW w:type="auto" w:w="0"/>
                <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
            </w:tblPr>
            <w:tblGrid>
                <w:gridCol w:w="3070"/>
                <w:gridCol w:w="3071"/>
                <w:gridCol w:w="3071"/>
            </w:tblGrid>
            <w:tr w:rsidR="00153204" w:rsidTr="00153204">
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3070"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3071"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3071"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
            </w:tr>
            <w:tr w:rsidR="00153204" w:rsidTr="00153204">
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3070"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3071"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3071"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
            </w:tr>
            <w:tr w:rsidR="00153204" w:rsidTr="00153204">
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3070"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3071"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
                <w:tc>
                    <w:tcPr>
                        <w:tcW w:type="dxa" w:w="3071"/>
                    </w:tcPr>
                    <w:p w:rsidR="00153204" w:rsidRDefault="00153204"/>
                </w:tc>
            </w:tr>
        </w:tbl>

They are some IDs here, but these IDs will change if the user adds a table, moves it, ...

What you could do is to add that identifier yourself:

Add a particular replacement string to the tables (for example ID:1)

This adds this id in the w:tblCaption attribute:

    <w:tblPr>
        <w:tblStyle w:val="Grilledutableau"/>
        <w:tblW w:type="auto" w:w="0"/>
        <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
        <w:tblCaption w:val="ID:1"/>
    </w:tblPr>

To add this caption in word: Right-Click on the table->Properties->Text/Replacement

Put some text element before each table you want to identify

This adds the following xml before the table

    <w:p w:rsidR="006B0CC1" w:rsidRDefault="006B0CC1">
        <w:r>
            <w:t>ID :1</w:t>
        </w:r>
        <w:bookmarkStart w:id="0" w:name="_GoBack"/>
        <w:bookmarkEnd w:id="0"/>
    </w:p>

I would go with the first possibility as it's easy to read those properties and they are inside the table, so you only have to parse the table elements.

Upvotes: 2

Related Questions