Using win32com to MERGE and UNMERGE cells
<p>All,</p>

<p>I have a <strong>table</strong> inside a <strong>Word document</strong> that contains <strong>merged cells</strong>.  I would like to <strong>unmerge</strong> those cells using the <strong>win32com</strong> package.</p>

<p>An example of this is when ROW 1 contains 5 cells, and ROW 2 contains 6 cells.</p>

<p>Ideally, I would like to unmerge all merged cells in ROW 1 such that the unmerged cells line up with ROW 2 and such that the data is displayed in the left-most cell of the resulting unmerged range.</p>

<p><strong>Example:</strong></p>

<p><strong>(Merged)</strong></p>

<pre><code>+++++++++++++++++++++++++++++++++++++
|hi   |bye  |     |Hello      |none |
+++++++++++++++++++++++++++++++++++++
|1    |21   |23   |good |bye  |3    |
+++++++++++++++++++++++++++++++++++++
</code></pre>

<p><strong>(Unmerged)</strong></p>

<pre><code>+++++++++++++++++++++++++++++++++++++
|hi   |bye  |     |Hello|     |none |
+++++++++++++++++++++++++++++++++++++
|1    |21   |23   |good |bye  |3    |
+++++++++++++++++++++++++++++++++++++
</code></pre>

<p>In the table with <strong>merged</strong> cells, there are a total of <strong>11</strong> cells.  In the <strong>unmerged</strong> table there are <strong>12</strong> cells.</p>

<p>Any ideas on how to do this.  Documentation for the win32com module is pretty sparse, and what little appears to exist is blocked while I'm at work.</p>

<p>Help would be GREATLY appreciated.</p>

<p>+++--------------------------------------------------------------------------+++</p>

<p><strong>Extra Details:</strong></p>

<p>I'm bringing my data in like this:</p>

<pre><code>#Opens an instance of MS Word in the background, then accesses the referenced
#file.

path = "string containing directory name"

Word = win32.Dispatch("Word.Application")
Word.Visible = False
Word.Documents.Open(path)

#Creates a com element containing access to the document contents of the file referenced above
MT_doc = Word.ActiveDocument
</code></pre>

<p>I then grab the tables out of the file using the following code:</p>

<pre><code>#Determins the number of tables in the Word Document and outputs a table
#element to "table"
num_tables = MT_doc.Tables.Count
table = MT_doc.Tables
</code></pre>

<p>Where I get stuck is that:</p>

<pre><code>table(1).Rows(1).Cells.Count != table(1).Rows(2).Cells.Count
</code></pre>

<p>In this case the first row has 10 cells while the second row has 18 cells.  Without being able to split these merged cells, the rest of my code fails to execute.</p>

<p>I found a solution to my problem.  After a lot of searching it would appear that tables embedded inside of MS Word do NOT have a "merge" property associated with them.  However, they do have a "width" property associated with them.  Getting this using win32com looks something like this:</p>

<pre><code>#See above for definition of "table" - it's just a win32com COM module
#containing all the tables inside of an MS Word document.
width = table(1).Rows(1).Cells(i).Width
</code></pre>

<p>This returns a     type(width) = float for each cell.  This value can be compared with the width value of any cell above or below as required.  If the width value of any cell in question is NOT equal to the width of a cell above or below, then you know a given cell is formatted in such a way that potential exists for the embedded table to contain a cell formatted much like my original example cell layout.  By iterating over the cells above or below, it is possible to add the widths of these cells together until they are equal or approximately equal to each other.  For my code the cells must be equal to within one integer value of each other.  This is accomplished by using a round() function to avoid a scenario where a person opens the Word document, accidentally changes the width of a single cell, and then only fixes that cell width by eye [This happened to me initially and it was because I had accidentally set all the cell widths in my file to width = 60.  When I fixed the table layout, I didn't quite get one cell back where it belonged].</p>

<p>The implementation of the code discussed is rather lengthy and somewhat specific to my particular problem.  However, the concept of using cell widths NOT some "merged" property of the cell is broadly applicable and should be a useful approach for others with similar problems to take.</p>

python ms-word win32com