python报错:AttributeError: 'Document' object has no attribute 'save'
时间: 2023-08-05 22:11:04 浏览: 315
根据提供的引用内容来看,你遇到的错误是关于Python中的'AttributeError',具体地说是'PDFDocument'对象没有'set_parser'属性或者'Document'对象没有'save'属性。根据引用,问题可能是你在代码中注释掉了一些关键的行,导致出现了这个错误。根据引用,你可以在网上搜索关于Phyton PDFMiner版本差异的文章,找到解决这个错误的方法。根据引用,你可以尝试注释掉或删除一些不需要的代码行来解决这个问题。希望这些信息能帮助你解决报错问题。
相关问题
python docx AttributeError: 'Document' object has no attribute 'close'
错误信息"AttributeError: 'Document' object has no attribute 'close'"通常是当你尝试对已经关闭的`Document`对象调用`close()`方法时发生的。`Document`对象在使用完后应该通过`.save()`保存并调用`.close()`来关闭,但在某些情况下,比如你在`with`语句块内部操作文档后,它会被自动关闭,所以在`with`外部直接调用`close()`可能会遇到这个错误。
如果确信文档还没有被关闭,你可以检查是否在正确的上下文中使用了`Document`,如上述提到的:
```python
doc = Document('example.docx')
try:
# ...对文档进行操作...
finally:
doc.save('updated_example.docx')
```
在这个例子中,即便有异常发生,在`finally`块中依然会调用`save()`并确保文件被关闭。如果`with`语句在预期范围内,那么这个错误就不应该出现。若仍有疑惑,建议检查代码中的`with`结构,确保文档在不需要时已经被正确地关闭了。
还报错啊AttributeError: 'Glyph' object has no attribute '_glyph'
非常抱歉,我之前的代码有误。确实,'Glyph'对象没有'_glyph'属性。请使用以下更新后的代码:
```pythonfrom fontTools.ttLib import TTFontimport svgwritedef convert_glyphs_to_svg(ttf_file):
font = TTFont(ttf_file)
glyphs = font.getGlyphSet()
glyf_table = font['glyf']
for glyph_name in glyphs.keys():
if glyph_name.startswith("glyph"):
glyph = glyphs[glyph_name]
unicode_value = glyph.getUnicode()
if unicode_value:
svg_file_name = f"{unicode_value}.svg"
svg_document = svgwrite.Drawing(svg_file_name)
glyph.draw(svg_document, glyf_table)
svg_document.save()
# 示例调用convert_glyphs_to_svg("path/to/your.ttf")
```
在上述代码中,我们使用`TTFont`加载ttf文件,并通过`getGlyphSet()`方法获取字体的所有字形。然后遍历每个字形,判断是否以"glyph"开头,并获取对应的Unicode值。
如果字形的Unicode值存在,我们将其作为svg文件的名称,并使用`svgwrite.Drawing()`创建一个新的svg文档。然后使用`draw()`方法将字形绘制到svg文档中,传入`glyf_table`作为参数。最后使用`save()`方法保存svg文件。
请确保将代码中的`"path/to/your.ttf"`替换为你想要转换的ttf文件的实际路径,然后运行代码即可。它将在当前目录下生成多个以Unicode值命名的svg文件,每个文件对应一个字形的svg表示。
阅读全文
相关推荐
















