解决Python的Aspose.Words FOSS问题
导入错误
ModuleNotFoundError: No module named 'aspose_font'
这个软件包在活跃的Python环境中没有安装. 安装它:
pip install "aspose-font>=1.0.0"如果使用一个,请确保安装到正确的虚拟环境中.
ImportError: cannot import name 'FontType' from 'aspose_font'
FontType 经过高层出口. aspose_font 使用:
from aspose_font import FontType # correct如果您看到这个错误,请确认已使用此文件. aspose-font>=1.0.0 在您的活动环境中安装.
字体加载错误
FontLoadException 在打电话时 FontLoader.open()
可能的原因:
- 文件路径不正确或没有存在.
- 文件损坏或有不标准的标题.
- 你传递了一个不合适字体的字节对象.
在加载之前检查文件是否存在:
import os
from aspose_font.loader import FontLoader
path = "MyFont.ttf"
if not os.path.exists(path):
raise FileNotFoundError(path)
font = FontLoader.open(path)窗口路径反斜线问题
在 Windows 上使用原始字符串或前方斜线:
font = FontLoader.open(r"C:\Fonts\MyFont.ttf")
# or
font = FontLoader.open("C:/Fonts/MyFont.ttf")API错误
AttributeError: 'Font' object has no attribute 'ttf_tables'
ttf_tables 是一个房产在 TtfFont, 不是基层. Font 使用类型注释:
from aspose_font.ttf.font import TtfFont
ttf_font: TtfFont = FontLoader.open("MyFont.ttf")
tables = ttf_font.ttf_tablesNotImplementedError 于 年 月 日 Font.to_bytes()
Font.to_bytes() 提高 NotImplementedError 只有在抽象基础类上调用时. 所有具体字体类 (TtfFont, CffFont, WoffFont, Woff2Font,实施) 的情况. to_bytes() 返回字体字节. 如果你打电话给 FontLoader.open() 你得到一个混凝土. 电话 to_bytes() 直接使用或使用 FontConverter.convert() 首先更改格式:
from aspose_font.converter import FontConverter
from aspose_font import FontType
woff2 = FontConverter.convert(font, FontType.WOFF2)