【问题标题】:rst2pdf : Generate pdf with formulasrst2pdf : 使用公式生成 pdf
【发布时间】:2014-06-16 16:18:47
【问题描述】:

我需要使用公式生成报告。我找到了图书馆 rst2pdf。我喜欢使用该库,但是在使用公式生成 pdf 时出现问题。要生成公式,我使用数学角色。以下代码不起作用。错误发生在模块 PIL 中。如何解决。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rst2pdf.createpdf import RstToPdf

mytext = u"""
================
Name of document
================

Title
---------

====================  ==========  ==========
Header row, column 1  Header 2    Header 3
====================  ==========  ==========
body row 1, column 1  column 2    column 3
body row 2, column 1  column 2    column 3
body row 3, column 1  column 2    column 3
====================  ==========  ==========

:math:`\\frac{1}{\\sigma\\sqrt{2\\pi}}\\exp\\left(-\\frac{(x-\\mu)^2}{2\\sigma^2}\\right) = 123`

"""

pdf = RstToPdf()
pdf.createPdf(text = mytext, output='foo.pdf')

脚本的输出

File "C:\Python27\lib\site-packages\PIL\Image.py", line 1549, in save
    raise KeyError(ext) # unknown extension
KeyError: '.png'

【问题讨论】:

  • 这似乎是一个与 rst2pdf 无关的 PIL 错误。如果你这样做from PIL import Image会发生什么?
  • 感谢回复,模块导入无误。

标签: python math python-imaging-library rst2pdf


【解决方案1】:

当 PIL/Pillow 无法识别选择保存的文件扩展名时会发生此错误。

from PIL import Image
im = Image.new("RGB", (100, 100))
im.save("test", ".png")

给予

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib/python3.7/site-packages/PIL/Image.py", line 1939, in save
     save_handler = SAVE[format.upper()]
KeyError: '.PNG'

这是因为“.png”不是有效格式,“png”是。你需要做的是

im.save("test", "png")

im.save("test.png")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2021-09-08
    • 2011-07-27
    • 2014-07-03
    • 2020-01-08
    • 2017-08-14
    • 2011-06-29
    相关资源
    最近更新 更多