2013-10-31 2 views
2

Python Reportlab를 인쇄 할 수 없습니다 :파이썬 Reportlab 특수 문자

pdf

# -*- coding: utf-8 -*- 
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer 
from reportlab.lib.styles import getSampleStyleSheet 
from reportlab.rl_config import defaultPageSize 
from reportlab.lib.units import inch 

styles = getSampleStyleSheet() 

def myFirstPage(canvas, doc): 
    canvas.saveState() 

def go(): 
    doc = SimpleDocTemplate("phello.pdf") 
    Story = [Spacer(1,2*inch)] 
    Story.append(Paragraph("Some text", styles["Normal"])) 
    Story.append(Paragraph("Some other text with &", styles["Normal"])) 
    doc.build(Story, onFirstPage=myFirstPage) 

go() 
"&"처럼 출력을

Some text 
Some other text with & 

그러나 다음과 기대에 특수 문자를 인쇄하는 동안이 문제에 직면하고있다

출력은

입니다.

'&'은 어디에서 사라졌습니까?

일부 포럼을 검색하여 &으로 인코딩해야하지만 각 특수 문자를 인코딩하는 것보다 쉬운 방법은없는 것일까 요?

내 스크립트의 상단에 "# -*- coding: utf-8 -*-"을 추가 한하지만 당신은 &amp;, &lt;&gt;으로> &, <를 교체해야 내 문제

+2

에 오신 것을 환영합니다 :

from cgi import escape Story.append(Paragraph(escape("Some other text with &"), styles["Normal"])) 

그러나, HTML 태그가 너무 일반적인 사용은 더 같은 것> <을 실제이 필요하고 : 그 작업을 수행하는 쉬운 방법 중 하나는 파이썬 탈출 기능입니다! 우리가 당신을 위해 몇 가지 코드를 작성하기를 원하는 것처럼 보입니다. 많은 사용자가 조난중인 코더 코드를 기꺼이 만들어내는 반면, 포스터는 대개 독자적으로 문제를 이미 해결하려고 시도했을 때만 도움이됩니다. 좋은 결과를 얻으려면 지금까지 작성한 코드 인 예제 입력 (있는 경우), 예상 출력 및 실제로 얻은 결과 (콘솔 출력, 스택 추적, 컴파일러 오류 - 해당되는 경우). 자세한 내용을 입력할수록 더 많은 답변을받을 수 있습니다. [FAQ] 및 [ask] –

+0

[보고서 랩에서 히브리어 (유니 코드)를 처리 할 수 ​​없음] (http://stackoverflow.com/questions/10958904/report-lab-cant-handle-hebrew-unicode)의 가능한 복제본을 확인하십시오. – sdasdadas

+0

sdasdadas가 없습니다. 그 스레드에서 주어진 해결책은 내 문제를 해결하지 못한다. –

답변

3

가 해결되지 않습니다. 스택 오버플로

text = "Some other text with &" 
Story.append(Paragraph(escape("<b>" + text + "</b>"), styles["Normal"]))