python docx 表格字体,Python-Docx表-更改字体的方法吗?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值