【发布时间】:2017-09-10 20:24:04
【问题描述】:
我有一些字体(Times New Roman,14),我需要测量字符串宽度。我从 API 中找到了 matplotlib 和示例:
from matplotlib import rcParams
import os.path
afm_filename = os.path.join(rcParams['datapath'], 'fonts', 'afm',
'ptmr8a.afm')
from matplotlib.afm import AFM
afm = AFM(open(afm_filename))
afm.string_width_height('What the heck?')
但我不明白应该粘贴什么而不是“datapaths”、“fonts”、“afm”。 这是在Python中获得刺痛宽度的方法吗?也许另一种方式?
【问题讨论】:
-
字符串宽度?你的意思是字符串的长度?
-
问题是,你的文件在哪里?
-
你的意思是以像素为单位的字符串长度吗?
-
我的问题是在 Word 中组织一些文本。所以,我使用 python-docx 来阅读 docx 文档。我需要在文档中测量以厘米为单位的字符串宽度。如果我使用默认设置(在上面的代码中):
-
Error: if not line.startswith(b'StartFontMetrics'): TypeError: startswith first arg must be str or a tuple of str, not bytes