Python-Docx表-更改字体的方法吗? [英] Python-Docx tables - way to change font?

查看:266
本文介绍了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.

我已经看到了一些类似的问题,人们指向使用xml,但是我不明白如何将其写入标准docx表代码中.

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!

推荐答案

有几种不同的方法,但是使用当前版本的python-docx最简单的方法是将段落样式应用于每个表格单元格.您需要在开始文档中已有样式更紧凑的字体,如此处所述: http://python-docx.readthedocs.org/en/latest/user/styles.html

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.

这篇关于Python-Docx表-更改字体的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆