我正在尝试使用reportlab汇总pdf文档。当我尝试写出一行<或者>符号文本消失,因为reportlab的段落支持xml标记。如何禁用此行为?我的很多文本都是包含<或者>符号这是我正在谈论的一个例子......
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
styles = getSampleStyleSheet()
def page_one(canvas, doc):
canvas.saveState()
canvas.setFont('Helvetica',12)
canvas.restoreState()
def main():
doc = SimpleDocTemplate("example.pdf")
Story = list()
style = styles["Normal"]
example_disappear = u'this is visible <this text is not visible> this is visible'
p = Paragraph(example_disappear, style)
Story.append(p)
doc.build(Story, onFirstPage=page_one)
if __name__ == "__main__":
main()
我希望能够使用&lt;或者&gt;没有文字的符号就会消失。
编辑: 马蒂诺让我走上正轨。我查看了一些通过python可用的xml实用程序,并找到了一些解析器。 python xml模块有一个转义函数,所以我刚添加了这一行...
from xml.sax.saxutils import escape
并将我的段落修改为......
p = Paragraph(escape(example_disappear), style)
文字可见。