本文介绍了从作为 string 可用的类实例化 python 类,仅在内存中!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Reportlab 创建 PDF.我正在创建两个 PDF,我想在创建它们后合并它们.Reportlab 提供了一种保存 pycanvas 的方法 (来源)(基本上是我内存中的 pdf 文件)作为 python 文件,并在该 python 文件上调用 doIt(filename) 方法,将重新创建 pdf 文件.这很棒,因为您可以在源代码的基础上组合两个 PDF 并创建一个合并 pdf.

I'm using Reportlab to create PDFs. I'm creating two PDFs which I want to merge after I created them. Reportlab provides a way to save a pycanvas (source) (which is basically my pdf file in memory) as a python file, and calling the method doIt(filename) on that python file, will recreate the pdf file. This is great, since you can combine two PDFs on source code basis and create one merge pdf.

这是这样做的:

from reportlab.pdfgen import canvas, pycanvas
#create your canvas
p = pycanvas.Canvas(buffer,pagesize=PAGESIZE)
#...instantiate your pdf...

# after that, close the PDF object cleanly.
p.showPage()
p.save()

#now create the string equivalent of your canvas
source_code_equiv = str(p)
source_code_equiv2 = str(p)

#merge the two files on str. basis
#not shown how it is exactly done, to make it more easy to read the source
#actually one just have to take the middle part of source_code_equiv2 and add it into source_code_equiv
final_pdf = source_code_equiv_part1 + source_code_equiv2_center_part + source_code_equiv_part2

#write the source-code equivalent of the pdf
open("n2.py","w").write(final_pdf)
from myproject import n2
p = n2.doIt(buffer)

# Get the value of the StringIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response

这工作正常,但我想跳过将 n2.py 保存到磁盘的步骤.因此,我正在寻找一种方法来从 final_pdf 字符串实例化相应的 python 类并直接在源代码中使用它.这可能吗?

This works fine, but I want to skip the step that I save the n2.py to the disk. Thus I'm looking for a way to instantiate from the final_pdf string the corresponding python class and use it directly in the source. Is this possible?

它应该像这样工作..

n2 = instantiate_python_class_from_source(final_pdf)
p = n2.doIt(buffer)

这样做的原因主要是没有真正需要将源保存到磁盘,其次绝对不是线程保存.我可以在运行时命名创建的文件,但是我不知道要导入什么!?如果没有办法阻止文件保存,有没有办法根据文件名定义导入,是运行时定义的!?

The reason for this is mainly that there is not really a need to save the source to the disk, and secondly that it is absolutely not thread save. I could name the created file at run time, but then I do not know what to import!? If there is no way to prevent the file saving, is there a way to define the import based on the name of the file, which is defined at runtime!?

有人可能会问我为什么不提前创建一个 pdf,但这是不可能的,因为它们来自不同的应用程序.

One might ask why I do not create one pdf in advance, but this is not possible, since they are coming from different applications.

推荐答案

这对于您想要的东西似乎还有很长的路要走.Reportlab 没有可以从中提取 PDF 文档的 Canvas 类吗?我不明白为什么这里应该涉及生成的 Python 源代码.

This seems like a really long way around to what you want. Doesn't Reportlab have a Canvas class from which you can pull the PDF document? I don't see why generated Python source code should be involved here.

但是如果出于某种原因需要,那么您可以使用 StringIO 将源写入"到一个字符串中,然后 exec 执行它:

But if for some reason it is necessary, then you can use StringIO to "write" the source to a string, then exec to execute it:

from cStringIO import StringIO

source_code = StringIO()
source_code.write(final_pdf)
exec(source_code)
p = doIt(buffer)

这篇关于从作为 string 可用的类实例化 python 类,仅在内存中!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:50