如果LaTeX太大,可以使用
Inline::Python
来包装ReportLab,这是大家都非常喜欢的(我没有使用过,对Python也不是很熟练)。编辑3:这里是编辑2,但是采用了模块化的风格,如果人们喜欢它(并且它具有某种稳健性),那么我可以将其发布到CPAN。现在请将
.pm
文件放在类似于
Inline/Python/ReportLab.pm
的文件结构中,放在您的
@INC
中的某个位置(脚本自己的基本目录通常在
@INC
中)。
package Inline::Python::ReportLab;
use strict;
use warnings;
use Carp;
use Inline::Python qw/py_eval/;
our @ISA = 'Inline::Python::Object';
sub import {
py_eval('from reportlab.pdfgen.canvas import Canvas');
}
sub new {
my $class = shift;
my $filename = shift || croak "Must specify file name to contructor";
return bless(Inline::Python::Object->new('__main__', 'Canvas', $filename), $class);
}
1;
然后,脚本可以是这样的:
use strict;
use warnings;
use Inline::Python::ReportLab;
my $c = Inline::Python::ReportLab->new('hello.pdf');
$c->drawString(100,100,"Hello World");
$c->showPage();
$c->save();
编辑2:
虽然编辑1仍然有趣,但似乎(如果我错误了,请告诉我!)我已经找出如何创建“Canvas”的实例并直接公开其方法:
use strict;
use warnings;
use Inline::Python qw/py_eval/;
py_eval('from reportlab.pdfgen.canvas import Canvas');
my $c = Inline::Python::Object->new('__main__', 'Canvas', 'hello.pdf');
$c->drawString(100,100,"Hello World");
$c->showPage();
$c->save();
编辑2/3:这部分内容是手动界面的示例。我认为编辑2/3提供了更好的界面,可以让原始的Python类承担重要任务,而不需要(太多)包装。
编辑1:我现在已经通过手动添加方法来公开一些功能。这意味着对于每个想要使用的方法,都必须添加一个包装器方法。虽然这已经是一个可行的解决方案,但我想知道是否有一种更简单的方法来公开整个Python“画布”类,但现在我只能到这里:
use strict;
use warnings;
use Inline Python => <<END_PYTHON;
from reportlab.pdfgen import canvas
class Canvas:
def __init__(self,filename):
self.canvas = canvas.Canvas(filename)
def drawString(self,x,y,text):
self.canvas.drawString(x,y,text)
def save(self):
self.canvas.showPage()
self.canvas.save()
END_PYTHON
my $c = Canvas->new('hello.pdf');
$c->drawString(100,100,"Hello World");
$c->save();
Inline::Python
使用吗? - Joel BergerInline
模块,该模块最近一次更新是在2011年2月。实际上,Inline::Python的更新时间是在2011年7月。我想没有必要担心。 - Joel Berger