Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 cyrillic symbols in tables in reportlab.
#1
Hello! I want to use Cyrillic in tables, but reportlab outputs black squares instead of characters. Here is an example code:
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

def table_fonts():
    doc = SimpleDocTemplate("table_fonts.pdf", pagesize=letter)
    story = []
    data = [['ф_{}'.format(x) for x in range(1, 6)],
                ['а', 'ф', 'ф', 'ф', 'ф']
                ]
    tblstyle = TableStyle([('TEXTFONT', (0, 0), (0, 1), 'FreeSans') ]) 
                                   
    tbl = Table(data)
    tbl.setStyle(tblstyle)
    story.append(tbl)
    doc.build(story)
if __name__ == '__main__':
    pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf')) 
    table_fonts()
Python 3.5.3, reportlab 3.5.13
Quote
#2
see: http://code.activestate.com/recipes/4388...f-library/
Quote
#3
(Mar-19-2019, 07:39 PM)Larz60+ Wrote: see: http://code.activestate.com/recipes/4388...f-library/

Thanks for your answer! Unfortunately this does not work, the problem is not that there is no suitable font, I think,that there should be some other way to change the font in the table is not through canvas

I added this code to my, the one that led above, now it gives me just a blank sheet

If I'm going on and fix the font to a normal string that is printed by using canvas, it's all right and the characters are introduced, the problem is that whatever font I set in canvas, SimpleDocTemplate.build won't use it and I don't know why. Here is a sample code that prints the Cyrillic alphabet canvas

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont


canvas = Canvas("canvas.pdf", pagesize=A4)
pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf'))
canvas.setFont('FreeSans', 32)
canvas.drawString(10, 150, "Сам текст энкодед ин UTF-8")
canvas.drawString(10, 100, "Шревтом FreeSans!")
canvas.showPage()
canvas.save()
That's what I get. Everything works:
Here is the code from the first table, but with the addition of what I drop above:
That's what I get on the way out.
Quote
#4
It's working! It turned out that under registering the fonts, here is this row:
pdfmetrics.registerFont(TTFont('FreeSans', 'FreeSans.ttf'))
It picks up the font from the folder from which it is launched. This font wasn't there, so it didn't work. Why does it output Russian strings using DrawString when there is no font in the folder is a good question, but I have no answer. Here is an example of working code:
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
def table_fonts():
    doc = SimpleDocTemplate("table_fonts.pdf", pagesize=letter)
    story = []
    data = [['ф_{}'.format(x) for x in range(1, 6)],
    ['фф', 'ф', 'ф', 'ф', 'ф']
    ]
    tblstyle = TableStyle([('FONT', (0, 0), (-1, 1), 'DejaVuSerif', 24)
    ])
    tbl = Table(data)
    tbl.setStyle(tblstyle)
    story.append(tbl)
    doc.build(story)
if __name__ == '__main__':
    pdfmetrics.registerFont(TTFont('DejaVuSerif', 'DejaVuSerif.ttf'))
    table_fonts()
make sure that the “DejaVuSerif.ttf" is in the folder next to the file. All the blessings
Quote
#5
Thanks for coming back with how you solved it :)

I think there might be something else going on here. From what I've seen other places (such as: https://stackoverflow.com/a/52450839 or http://www.blog.pythonlibrary.org/2013/0...out-fonts/), reportlab has a big list of system directories it looks for fonts, so you shouldn't be required to have it in the same folder as the file you're running.

But, if it works, and you're happy, then it's GoodEnough™.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help! - How to create a Title for a Reportlab Table crabbylou 0 55 Mar-29-2020, 09:14 PM
Last Post: crabbylou
  ReportLab Polypop77 0 66 Mar-20-2020, 01:17 PM
Last Post: Polypop77
  Replacing symbols by " Tiihu 1 149 Feb-13-2020, 09:27 PM
Last Post: Larz60+
  How do I delete symbols in a list of strings? Than999 1 237 Nov-16-2019, 09:37 PM
Last Post: ibreeden
  Formatting cyrillic characters Truman 6 851 Jan-06-2019, 12:48 AM
Last Post: Truman
  Python Reportlab Wordwrap Table Mady 0 1,674 Dec-18-2018, 06:31 AM
Last Post: Mady
  Reportlab Dynamic Table Q Gutt 0 2,019 Jun-13-2018, 10:18 PM
Last Post: Gutt
  Reportlab: Add xlabel, ylabel and grid to lineplot denissanga 2 2,287 Dec-19-2017, 04:48 PM
Last Post: denissanga
  ReportLab ek5442 2 1,692 Aug-24-2017, 01:29 PM
Last Post: ek5442
  Python symbols AND letters gullidog 1 1,418 Apr-05-2017, 10:13 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)