[python] reportlab a čeština

Vladislav Ludík vlada na ludik.cz
Neděle Únor 4 08:58:37 CET 2007


Problém: latin2 v modulu reportlab

Když použiju program viz níže, jsou vygenerovány pdf tabulky v latin1.

Jak dostat z reportlab co nejjednodušeji českou sadu na výstupu pdf ?

import sys
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen import canvas
import string

label_formats = {'dec':('%d=', 'Decimal'),
                'oct':('%o=','Octal'),
                'hex':('0x%x=', 'Hexadecimal')}

def run(mode):

   label_formatter, caption = label_formats[mode]

   for enc in ['WinAnsi','MacRoman']:

       canv = canvas.Canvas(
               'StandardFonts_%s.pdf' % enc,
               )
       canv.setPageCompression(0)

       for faceName in pdfmetrics.standardFonts:
           if faceName in ['Symbol', 'ZapfDingbats']:
               encLabel = faceName+'Encoding'
           else:
               encLabel = enc + 'Encoding'

           fontName = faceName + '-' + encLabel
           pdfmetrics.registerFont(pdfmetrics.Font(fontName,
                                       faceName,
                                       encLabel)
                       )

           canv.setFont('Times-Bold', 18)
           canv.drawString(80, 744, fontName)
           canv.setFont('Times-BoldItalic', 12)
           canv.drawRightString(515, 744, 'Labels in ' + caption)


           #for dingbats, we need to use another font for the numbers.
           #do two parallel text objects.
           for byt in range(32, 256):
               col, row = divmod(byt - 32, 32)
               x = 72 + (66*col)
               y = 720 - (18*row)
               canv.setFont('Helvetica', 14)
               canv.drawString(x, y, label_formatter % byt)
               canv.setFont(fontName, 14)
               canv.drawString(x+44, y, 
chr(byt).decode(encLabel,'ignore').encode('utf8'))
                         canv.showPage()
       canv.save()


if __name__ == '__main__':

   mode = 'hex'
   run(mode)






Další informace o konferenci Python