如何在Python中用阿拉伯语reportlab绘制表格:

如何在Python中用阿拉伯语reportlab绘制表格:,python,mysql,python-3.7,reportlab,Python,Mysql,Python 3.7,Reportlab,以下面的代码打印表格时,阿拉伯语显示为符号。 多谢各位 import mysql.connector from reportlab.lib import colors from reportlab.lib.pagesizes import letter from reportlab.platypus import Paragraph, SimpleDocTemplate, Table, TableStyle from reportlab.lib.styles import getSampleSt

以下面的代码打印表格时,阿拉伯语显示为符号。 多谢各位

import mysql.connector
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import Paragraph, SimpleDocTemplate, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont


doc = SimpleDocTemplate("Ropert_Accont.pdf", pagesize=letter)
pdfmetrics.registerFont(TTFont('Arabic', 'Arial.ttf'))
styles = getSampleStyleSheet()

elements = []

db = mysql.connector.connect(user='root', passwd='', host='localhost', database='users')
cursor = db.cursor()
cursor.execute("SELECT * FROM acoonus  WHERE  idus = 1")
result = cursor.fetchall()

dt = [('ت', 'المبلغ', 'تاريخ التسليم', 'الملاحضات')]
for rus in result:
    dt = dt + [rus]

t = Table(dt)
t.setStyle(TableStyle([('ALIGN', (0, 0), (-1, 0), 'CENTER'),
                       ('TEXTCOLOR', (0, 0), (-1, 0), colors.black),
                       ('BACKGROUND', (0, 0), (-1, 0), colors.green),
                       ('TEXTCOLOR', (0, 0), (0, -1), colors.red),
                       ('VALIGN', (0, -1), (-1, -1), 'MIDDLE'),
                       ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
                       ('BOX', (0, 0), (-1, -1), 0.5, colors.black),
                       ('TEXTfont', (0, 0), (-1, -1), "Arabic")
                       
                       ]))

elements.append(t)

doc.build(elements)

是的,您可以在表格中使用阿拉伯语,方法是在样式中注册阿拉伯语字体并在表格中使用它

您所做的只是编写了很棒的代码,唯一缺少的是添加带有“FONTNAME”的字体样式,而不是“TEXTfont

要显示阿拉伯语单词,你应该先反转,检查下面的代码可能会有所帮助

import arabic_reshaper
from bidi.algorithm import get_display

pdf.registerFont(TTFont('Arabic', 'path to the font directory/cairo.ttf'))
tabledata = [
    [get_display(arabic_reshaper.reshape(u"الاسم")), get_display(arabic_reshaper.reshape(u"الهاتف"))],
    [get_display(arabic_reshaper.reshape(u"تجربة")), "123456789"]
]
t1 = Table(tabledata, 4 * [1.8 * inch], 4 * [0.5 * inch])
t1.setStyle(TableStyle([
    ('FONTNAME', (0, 0), (3, 3), "Arabic"),
]))
正如你所看到的,你可以通过这个代码来反转垃圾

ar = arabic_reshaper.reshape(u"السلام عليكم")
ar = get_display(ar)

是的,您可以在表格中使用阿拉伯语,方法是在样式中注册阿拉伯语字体并在表格中使用它

您所做的只是编写了很棒的代码,唯一缺少的是添加带有“FONTNAME”的字体样式,而不是“TEXTfont

要显示阿拉伯语单词,你应该先反转,检查下面的代码可能会有所帮助

import arabic_reshaper
from bidi.algorithm import get_display

pdf.registerFont(TTFont('Arabic', 'path to the font directory/cairo.ttf'))
tabledata = [
    [get_display(arabic_reshaper.reshape(u"الاسم")), get_display(arabic_reshaper.reshape(u"الهاتف"))],
    [get_display(arabic_reshaper.reshape(u"تجربة")), "123456789"]
]
t1 = Table(tabledata, 4 * [1.8 * inch], 4 * [0.5 * inch])
t1.setStyle(TableStyle([
    ('FONTNAME', (0, 0), (3, 3), "Arabic"),
]))
正如你所看到的,你可以通过这个代码来反转垃圾

ar = arabic_reshaper.reshape(u"السلام عليكم")
ar = get_display(ar)

也许这会很鼓舞人心:也许这会很鼓舞人心: