python 当从阿拉伯语数据库表导入数据时,reportlab library在pdf中颠倒字母

ffscu2ro  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(209)

我试图从数据库中导入阿拉伯语数据并将其显示到表中,使用reportlab库,但输出显示为单独和反转的字母

这是我的代码的一部分:

import arabic_reshaper
from bidi.algorithm import get_display
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import sqlite3
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import Image, Paragraph, SimpleDocTemplate, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

def onFirstPage(canvas, document):
    arabic_text = arabic_reshaper.reshape(u'بيان')
    arabic_text = get_display(arabic_text)
    pdfmetrics.registerFont(TTFont('Arabic-bold', 'tradbdo.ttf'))

    canvas.setFont('Arabic-bold', 14)
    canvas.drawString(50, 750,arabic_text)
    canvas.line(50, 740, 75, 740)

doc = SimpleDocTemplate("Report.pdf", pagesize=letter)
elements = []

styleSheet = getSampleStyleSheet()

P0 = Paragraph('''
A paragraph
1''',
styleSheet["BodyText"])
P = Paragraph(''' The ReportLab Left
Logo
Image''',
styleSheet["BodyText"])
pdfmetrics.registerFont(TTFont('Arabic-bold', 'tradbdo.ttf'))
data = []
conn = sqlite3.connect("S:\الموارد البشرية & الرعاية الطبية\HR1.db")
c = conn.cursor()
c.execute('SELECT number, name, date1, date2 FROM agazat2')
for row in c.fetchall():
    data.append(row)
conn.close()
c_width=[0.5*inch,3*inch,1.5*inch,1.5*inch]
t=Table(data, rowHeights=20,repeatRows=1,colWidths=c_width, style=[('GRID',(0,0),(-1,-1),1,colors.black),
])
t.setStyle(TableStyle([
    ('FONTNAME', (0, 0), (-1, -1), "Arabic-bold"),
]))

elements.append(t)
doc.build(elements, onFirstPage=onFirstPage)

4.我试了很多方法来解决这个问题,但还是找不到解决办法

oprakyz7

oprakyz71#

你必须使用Arabic-shaper + python-bidi软件包来修复这个问题。2看这里了解更多细节:https://abderrahmane.hashnode.dev/how-to-add-arabic-text-on-pdf-with-reportlab

相关问题