Python-Docx tables - way to change font?

元气小坏坏 提交于 2019-12-25 04:44:23

问题


I am using Python-Docx to allow users of my code to create tables. I would like to change the font size of the table. The cell value numbers are wrapping around in the table cells because the font is too large. I have searched the documentation and many forums without any luck so far.

Am i correct in my assumption that only table styles can be changed, but not font sizes?

I realize I could modify or create my own style, but that will not help other users of my code on other computers ((such as explained in forum post: How do I get rid of the default styling on a table object in a python-docx generated word document?)) Most other posts that I found discuss style, but not font size.

I have seen some similar questions where people point to using xml, but I do not understand how this would be written into the standard docx table code.

Any help or insights greatly appreciated!


回答1:


There are a few different approaches, but the simplest you can do with the current version of python-docx is to apply a paragraph style to each of the paragraphs in each table cell. You'll need to have the style with the more compact font already in your starting document as detailed here: http://python-docx.readthedocs.org/en/latest/user/styles.html

Here's an aircode example that should get you going in the right direction:

table = document.add_table(...)
for row in table.rows:
    for cell in row.cells:
        for paragraph in cell.paragraphs:
            paragraph.style = 'CellText'  # assuming style named "Cell Text"

Respond in a comment if any of this doesn't make sense. Btw, the next version will have substantial new features for styles and direct font property application, e.g. size and typeface. It should be out in a month or so.



来源:https://stackoverflow.com/questions/27851157/python-docx-tables-way-to-change-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!