0

csv 入力から pdf ファイルをレンダリングするテンプレートを作成しました。ただし、csv 入力フィールドに改行やインデントを含むユーザー書式が含まれていると、rst2pdf 書式設定エンジンが混乱します。ドキュメント フローを壊さずに、入力テキストの書式設定を維持しながら、一貫してユーザー入力を処理する方法はありますか? 以下のスクリプト例:

from mako.template import Template
from rst2pdf.createpdf import RstToPdf

mytext = """This is the first line
Then there is a second
Then a third
   This one could be indented

I'd like it to maintain the formatting."""

template = """
My PDF Document
===============

It starts with a paragraph, but after this I'd like to insert `mytext`. 
It should keep the formatting intact, though I don't know what formatting to expect.

${mytext}

"""

mytemplate = Template(template)
pdf = RstToPdf()
pdf.createPdf(text=mytemplate.render(mytext=mytext),output='foo.pdf')

テンプレートに次の関数を追加して|各行の先頭に挿入しようとしましたが、それもうまくいかないようです。

<%!
def wrap(text):
    return text.replace("\\n", "\\n|")
%>

その後${mytext}、 になり|${mytext | wrap}ます。これはエラーをスローします:

<string>:10: (WARNING/2) Inline substitution_reference start-string without end-string.
4

1 に答える 1

0

実際、私は正しい軌道に乗っていたことがわかりました|. とテキストの間にスペースが必要だっただけです. したがって、次のコードが機能します。

from mako.template import Template
from rst2pdf.createpdf import RstToPdf

mytext = """This is the first line
Then there is a second
Then a third
    How about an indent?

I'd like it to maintain the formatting."""

template = """
<%!
def wrap(text):
    return text.replace("\\n", "\\n| ")
%>

My PDF Document
===============

It starts with a paragraph, but after this I'd like to insert `mytext`. 
It should keep the formatting intact.

| ${mytext | wrap}

"""

mytemplate = Template(template)
pdf = RstToPdf()
#print mytemplate.render(mytext=mytext)
pdf.createPdf(text=mytemplate.render(mytext=mytext),output='foo.pdf')
于 2012-11-15T06:45:37.120 に答える