简体   繁体   中英

Python-Docx tables - way to change font?

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!

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, eg size and typeface. It should be out in a month or so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM