从Python输出文件(最好是PDF) [英] Output a Document (preferably a PDF) from Python

查看:438
本文介绍了从Python输出文件(最好是PDF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本(3.5),它将通过一系列的测试.用户可以从N个可能的测试中选择要用完的测试.因此,用户最多可以运行1个测试,最多可以进行N个测试.

I've got a Python script (3.5) that will go through a whole battery of tests. The user gets to select which tests get run out of N possible tests. So, the user could run 1 tests up to N tests.

现在,我只是将测试结果输出到带有matplotlib的图上,看起来还可以,但是它们只是保存为单独的文件.此外,该代码对每个测试都有一些通过/失败的条件.

Right now, I'm just outputting the results of the test to a plot with matplotlib and that looks OK, but they're just saved as individual files. Also, the code has some PASS/FAIL criteria for each test.

所以,问题是,我想在Python中使用某种工具将测试序列的整个故事输出为PDF.我希望能够保留一些Boiler-Plate的内容并在中间更新一些内容.例如:

So, the issue is, I would like to use some tool with Python to output the whole story of the test sequence as a PDF. I'd like to be able to keep some Boiler-Plate stuff and update some stuff in the middle....For example:

Test was run for 7 minutes. Maximum power was recorded as -69.5dBm. 每次相同的是:

Test was run for minutes. Maximum power was recorded as dBm.

然后,从测试结果中提取分钟数和最大功率数.此外,该图是从"matplotlib"中提取的.

And, the number of minutes and the number for maximum power is pulled in from the results of the test. Also, the graph is pulled in from 'matplotlib'.

因此,对于每个测试,我想添加一些样板文字,用真实数据填充一些空白,并在适当的地方拍上一张图像,我想用Python做到这一点.

So, for each test, I'd like to append some Boiler-plate text, fill in some blanks with real data, and slap in an image where appropriate, and I'd like to do it with Python.

我查看了一些关于SO的建议,但是在大多数情况下,解决方案似乎是为现有 PDF附加或加水印.此外,Google为自动生成Python代码文档提供了很多结果,而不是通过Python生成文档的代码.

I looked at some of the suggestions on SO, but for the most part it looks like the solutions are for appending or watermarking existing PDFs. Also, Google turns up a lot of results for automating the generation of Python Code Documentation...Not Code for generating Documentation with Python.

Reportlab看起来很有前途,但似乎已经被放弃了.

Reportlab looked promising, but it looks like it's been abandoned.

此外,我不满足于输出为PDF的要求.这仅用于内部使用,因此具有一定的灵活性. HTML,Word或其他随后可以由用户手动转换为PDF的内容也可以.我知道PDF由于其二进制性质而有些麻烦.

Also, I'm not married to the requirement that the output be a PDF. This is for internal use only, so there's some flexability. HTML, Word or something else that can be converted to a PDF manually by the user afterwards is fine too. I know PDFs can be somewhat troublesome due to their binary nature.

推荐答案

您可以直接在matplotlib中完成所有这些操作. 可以创建多个图形,然后使用matplotlib.backends.backend_pdf.PdfPages将它们全部保存到同一pdf文档中. 可以使用text()命令设置文本.

You can do all of this directly in matplotlib. It is possible to create several figures and afterwards save them all to the same pdf document using matplotlib.backends.backend_pdf.PdfPages.
The text can be set using the text() command.

这是一个如何工作的基本示例.

Here is a basic example on how that would work.

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np

N=10
text =  "The maximum is {}, the minimum is {}"

# create N figures
for i in range(N):
    data = np.random.rand(28)
    fig = plt.figure(i+1)
    ax= fig.add_subplot(111)
    ax.plot(np.arange(28), data)
    # add some text to the figure
    ax.text(0.1, 1.05,text.format(data.max(),data.min()), transform=ax.transAxes)

# Saving all figures to the same pdf
pp = PdfPages('multipage.pdf')
for i in range(N):
    fig = plt.figure(i+1)
    fig.savefig(pp, format='pdf')
pp.close()

这篇关于从Python输出文件(最好是PDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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