Python reportlab';中未正确包装阿拉伯文文本;s段可流动

Python reportlab';中未正确包装阿拉伯文文本;s段可流动,python,arabic,right-to-left,reportlab,paragraph,Python,Arabic,Right To Left,Reportlab,Paragraph,假设我有一段阿拉伯语片段: 在1573月15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半

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

在1573月15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半在2016年6月上,从从上下半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半半تتتتتتتتتتتتت

在英语中,这应该是这样的意思:“如果我们考虑到气候变化和年际变化的性质,以及长期的变化,加上所使用的测量和计算缺乏准确性……”

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

阿拉伯码>阿拉伯码码>阿拉伯码-阿拉伯码-阿拉伯码文本=u u方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方在2016年6 6月月月月日,本方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方㶕㶕㶕㶕㶕㶕㶕㶕㶕㶕㶡 阿拉伯文文本=阿拉伯文重塑器。重塑(阿拉伯文文本)#连接字符 阿拉伯文文本=获取显示(阿拉伯文文本)#使用bidi更改方向 pdf_file=open('disclaimer.pdf','w') pdf\u doc=SimpleDocTemplate(pdf\u文件,页面大小=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([段落(阿拉伯文、样式)]) pdf_file.close() 结果就在这里。您可以看到文本本身是正确的和可读的(至少对于Google Translate是这样),但没有按照RTL脚本的预期进行包装。

如果您使用

并从代码中删除此行:

arabic_text = get_display(arabic_text) # change orientation by using bidi
您的代码将正常运行

因为它们已经用PyFriBiDi解决了 如你所见:

来自社区的一些用户,特别是Ury Marshak、Moshe Wagner和Hosam Aly,已经提供了批量,让PyFribibibi与ReportLab一起工作。我们已经为这项开发创建了一个SVN分支机构,可在

(该页面上的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()

使用wordwrap模块和

标记拆分行;这并不完美,因为在每个段落的顶部都有一个空行,但它是一些用例的简单解决方案

import textwrap
def ShowArabictext(Text):


#style_comment.alignment = TA_RIGHT
wrkText=Text

isArabic=False
isBidi=False

for c in wrkText:
    cat=unicodedata.bidirectional(c)

    if cat=="AL" or cat=="AN":
        isArabic=True
        isBidi=True
        break
    elif cat=="R" or cat=="RLE" or cat=="RLO":
        isBidi=True

if isArabic:

    #wrkText=arabic_table(wrkText)    

    wrkText=textwrap.wrap( wrkText,70)
    wrkTexttemp=[]
    l=u''
    i=0
    for w in wrkText:
        # break each line with html markup allowed in reportlab 
        l=l+u'<br></br>'+arabic_rtlize.process.shape(arabic_reshaper.reshape(w ))

    wrkText=l



if isBidi:
    wrkText=get_display(wrkText)

return [wrkText,isArabic,isBidi]
导入文本包装
def ShowArabictext(文本):
#style_comment.alignment=TA_RIGHT
wrkText=文本
isArabic=假
isBidi=False
对于wrkText中的c:
cat=单向数据。双向(c)
如果cat==“AL”或cat==“AN”:
isArabic=True
isBidi=True
打破
elif cat==“R”或cat==“RLE”或cat==“RLO”:
isBidi=True
如果是isArabic:
#wrkText=阿拉伯文表格(wrkText)
wrkText=textwrap.wrap(wrkText,70)
wrkTexttemp=[]
l=u“
i=0
对于wrkText中的w:
#使用reportlab中允许的html标记打断每行
l=l+u'

'+阿拉伯文\u rtlize.process.shape(阿拉伯文\u reformer.reformate(w)) wrkText=l 如果是isBidi: wrkText=获取显示(wrkText) return[wrkText、isArabic、isBidi]
使用下面的代码。它会把它包好的

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
from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.lib.units import inch

def text_wraping(text,aW):
    text_width = stringWidth(text, style.fontName, style.fontSize)
    space_width = stringWidth(' ', style.fontName, style.fontSize)
    if text_width > aW:
        lines = []
        text = arabic_text.split(' ')
        text.reverse()
        line = ''
        for word in text:
            line_width = stringWidth(line, style.fontName, style.fontSize)
            word_width = stringWidth(word, style.fontName, style.fontSize)
            if (line_width < aW) and (line_width + word_width + space_width < aW):
                line += word + ' '
            else:
                line = line.split(' ')
                line.reverse()
                tmp = ' '
                line = tmp.join(line)
                lines.append(line)
                line = word + ' '
        line = line.split(' ')
        line.reverse()
        tmp = ' '
        line = tmp.join(line)
        lines.append(line)
    return(lines)

pdf_doc = SimpleDocTemplate('disclaimer.pdf', pagesize=A4)
pdfmetrics.registerFont(TTFont('Aims', 'Aims.ttf'))
style = ParagraphStyle(name='Normal', fontName='Aims', fontSize=12, leading=12. * 1.2,wordWrap='RTL')
style.alignment=TA_RIGHT
aW = A4[0]-2*inch  # change this line to the specified width for example paragraph width or table width
arabic_text = 'إذا أخذنا بعين الإعتبار طبيعة تقلب المناخ و المتغيرات البينية السنوية و تلك على المدى الطويل إضافة إلى عدم دقة القياسات والحسابات المتبعة'
arabic_text = arabic_reshaper.reshape(arabic_text) # join characters
arabic_text = get_display(arabic_text) # change orientation by using bidi
lines = text_wraping(arabic_text,aW)
elements = []
for line in lines:
    elements.append(Paragraph(line,style))
pdf_doc.build(elements)
导入阿拉伯语\u整形器
从bidi.算法导入获取显示
从reportlab.platypus导入SimpleDoctTemplate,第
从reportlab.pdfbase导入pdfmetrics
从reportlab.lib.styles导入段落样式
从reportlab.lib.enums导入TA_RIGHT
从reportlab.lib.pagesizes导入A4
从reportlab.pdfbase.TTFont导入TTFont
从reportlab.pdfbase.pdfmetrics导入stringWidth
从reportlab.lib.units导入英寸
def文本包装(文本,aW):
text_width=stringWidth(text,style.fontName,style.fontSize)
空格_width=stringWidth(“”,style.fontName,style.fontSize)
如果文本宽度>aW:
行=[]
text=阿拉伯文\u text.split(“”)
text.reverse()
行=“”
对于文本中的单词:
线宽=stringWidth(线,style.fontName,style.fontSize)
word_width=stringWidth(word,style.fontName,style.fontSize)
如果(线宽
我有点困惑(是我的错,不是你的错)。将路线设置为右侧。。。它是对的。我不懂阿拉伯语,所以可能是因为我不知道问题出在哪里(也就是说,它是否断裂错误?在你的标题中,你说它没有正确包装,但这是怎么回事?)。如果你把它改成英语,你会得到同样的结果吗?如果是这样,这可能会帮助我了解问题所在(我理解这与单词无关,但同样,我看不到它在哪里不正确地拆分/wr)