Table cell row heights shrink after processing a Word document with golang nguyenthenguyen/docx

Table of Contents

    I ran into a strange problem: after using the golang nguyenthenguyen/docx to manipulate a Word document, the table cell heights became smaller. This caused the generated HR system employee onboarding resume Word documents to fail to fill a single page, and when there are multiple pages it looks odd.

    Because I’m not very familiar with Word layout, I tried adjusting the row height but found it had no effect. I’m completely at a loss.

    Row height set to “fixed value”

    The solution given by Gemini was:

    In the Word template, right-click the table -> Table Properties -> Row, do not set “At least” (minimum), but set the row height to a “fixed value”. This way, regardless of how much content there is, the height will remain consistent.

    This seems perfectly reasonable and displays correctly in the template, but after golang processing the fixed row height disappears… After testing several times it still doesn’t work, so other solutions are needed.

    Increase line spacing

    In the end, I solved it by adjusting the line spacing. Changing the original single spacing to 1.5 spacing fixed it. That way, in the generated Word document the height matches the template exactly and the cell’s vertical space isn’t automatically compressed. The image below shows how to set line spacing in WPS:

    WPS line spacing settings

    Spacing before paragraph

    This method solves most scenarios; the only exception was one cell that wouldn’t vertically center even after setting vertical alignment. It was resolved by increasing the spacing-before value to 0.5. Set this in the line spacing advanced options as the spacing before the paragraph.

    WPS spacing before paragraph

    Introduction to nguyenthenguyen/docx

    Replacing placeholders in Word docx documents with golang

    About the Author 🌱

    I am a developer from Yantai, Shandong, China. If you have any interesting topics or software development needs, feel free to email me at: zhongwei.sun2008@gmail.com for a chat, or follow my personal public account "Elephant Tools", See more contact information