gpt4 book ai didi

python - 阿拉伯语文本未正确包装在 reportlab 的段落中

转载 作者:行者123 更新时间:2023-11-28 18:48:22 25 4
gpt4 key购买 nike

假设我有这段阿拉伯语片段:

إذاأخذstأخذأخذأخذأخذأخذأخذااإإططططlistianteطططط。

在英语中,这应该是这样的意思:“如果我们考虑到气候变率和年际变率的性质,以及那些长期缺乏测量和计算准确性的因素……。”

现在我想将其呈现为 Reportlab PDF 文档(python):

arabic_text = u'إذا أخذنا بعين الإعتبار طبيعة تقلب المناخ و المتغيرات البينية السنوية و تلك على المدى الطويل إضافة إلى عدم دقة القياسات والحسابات المتبعة'
arabic_text = arabic_reshaper.reshape(arabic_text) # join characters
arabic_text = get_display(arabic_text) # change orientation by using bidi

pdf_file=open('disclaimer.pdf','w')
pdf_doc = SimpleDocTemplate(pdf_file, pagesize=A4)
pdfmetrics.registerFont(TTFont('Arabic-normal', '../fonts/KacstOne.ttf'))
style = ParagraphStyle(name='Normal', fontName='Arabic-normal', fontSize=12, leading=12. * 1.2)
style.alignment=TA_RIGHT
pdf_doc.build([Paragraph(arabic_text, style)])
pdf_file.close()

结果在这里https://www.dropbox.com/s/gdyt6930jlad8id/disclaimer.pdf .您可以看到文本本身是正确且可读的(至少对于 Google 翻译而言),但未按 RTL 脚本的预期换行。

最佳答案

如果您使用 this branch of Report lab that adds RTL support

并从您的代码中删除这一行:

arabic_text = get_display(arabic_text) # change orientation by using bidi

您的代码将正常运行

因为它们已经通过使用 PyFriBiDi 的分支解决了如你所见here :

Some users from the community notably Ury Marshak, Moshe Wagner and Hosam Aly have contributed batches to get PyFriBibi working with ReportLab. We have created an SVN branch for this development and it is available at ...

(该页面上的 SVN 链接不再有效,因此您应该使用我提供的 bitbucket 链接!)

我已经成功地运行了你的代码的这个修改版本并产生了正确的结果:

from libs import arabic_reshaper
from bidi.algorithm import get_display
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_RIGHT
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase.ttfonts import TTFont
arabic_text = u'إذا أخذنا بعين الإعتبار طبيعة تقلب المناخ و المتغيرات البينية السنوية و تلك على المدى الطويل إضافة إلى عدم دقة القياسات والحسابات المتبعة'
arabic_text = arabic_reshaper.reshape(arabic_text) # join characters
# arabic_text = get_display(arabic_text) # change orientation by using bidi

pdf_file=open('disclaimer.pdf','w')
pdf_doc = SimpleDocTemplate(pdf_file, pagesize=A4)
pdfmetrics.registerFont(TTFont('Arabic-normal', 'fonts/misc/KacstOne.ttf'))
style = ParagraphStyle(name='Normal', fontName='Arabic-normal', fontSize=12, leading=12. * 1.2)
style.alignment=TA_RIGHT
pdf_doc.build([Paragraph(arabic_text, style)])
pdf_file.close()

关于python - 阿拉伯语文本未正确包装在 reportlab 的段落中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17166198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com