I have been trying to get new tables created with docx.Document.add_table to scale with the width of the contents, but they always appear to fill the entire space, regardless of the alignment that I specify. Basic code is:
from docx import Document
from docx.enum.table import WD_TABLE_ALIGNMENT
d = Document()
t = d.add_table()
t.alignment = WD_TABLE_ALIGNMENT.CENTER
d.save('test.docx')
The resulting table spans the margins. The XML looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" 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:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" 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:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:body>
<w:tbl>
<w:tblPr>
<w:tblW w:type="auto" w:w="0"/>
<w:jc w:val="center"/>
<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="4320"/>
<w:gridCol w:w="4320"/>
</w:tblGrid>
<w:tr>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="4320"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="4320"/>
</w:tcPr>
<w:p/>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="4320"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="4320"/>
</w:tcPr>
<w:p/>
</w:tc>
</w:tr>
</w:tbl>
<w:sectPr w:rsidR="00FC693F" w:rsidRPr="0006063C" w:rsidSect="00034616">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/>
<w:cols w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
Perhaps this is related to the contents of the <w:tblGrid> tag?
I am using python-docx version 0.8.5.
Doing for c in t._tbl.tblGrid.gridCol_lst: c.w = 0 made all the columns have zero width, but did not allow them to expand when I added a string to each of the cells.
The only way I have found to fix the issue is to go to Table Properties, Column tab, and uncheck the "Preferred Width" checkbox. Initially I thought that doing t._tbl.remove(t._tbl.tblGrid) would do that programatically, but it appears to make no difference. However, looking at the XML, the difference seems to be that for each column (w:tc) in each row (w:tr), the w:tcW tag reads <w:tcW w:w="0" w:type="auto"/> instead of <w:tcW w:type="dxa" w:w="4320"/> or some other non-auto value. This means that this issue is probably just a nuance of #161.
Given the above, this issue should probably be closed or merged with #161. In the meantime, I did find a hacky way to get around this issue that seems to work pretty well, especially if applied after the table is filled in (i.e., all columns and rows are added to avoid potential layout conflicts):
for r in t.rows:
for c in r._tr.tc_lst:
tcW = c.tcPr.tcW
tcW.type = 'auto'
tcW.w = 0
This manually sets the correct properties for each w:tcW tag in the XML. Hopefully this helps anyone who has the same issue before the changes are mare to python-docx.
Also, perhaps this is worth adding here: http://python-docx.readthedocs.io/en/latest/dev/analysis/index.html?
Most helpful comment
Given the above, this issue should probably be closed or merged with #161. In the meantime, I did find a hacky way to get around this issue that seems to work pretty well, especially if applied after the table is filled in (i.e., all columns and rows are added to avoid potential layout conflicts):
This manually sets the correct properties for each
w:tcWtag in the XML. Hopefully this helps anyone who has the same issue before the changes are mare to python-docx.Also, perhaps this is worth adding here: http://python-docx.readthedocs.io/en/latest/dev/analysis/index.html?