阿拉伯文本正在报告实验室中反转行。

huangapple go评论132阅读模式
英文:

Arabic text is revering lines in reportlab

问题

formatted_paragraph = Paragraph(bidirectional_text, paragraph_style)
return formatted_paragraph

arabic_text = ''وحدة1 وحدة2''

Here's the output:
وحدة2
وحدة1

Here's what I expected:
وحدة1
وحدة2

英文:

In reportLab, I need to display an arabic paragraph. I am using arabic-resharper and bidi-algorithm. The problem appears in the algorithm that reverses lines.

My code is:

def format_arabic_paragraph(arabic_text: str, paragraph_style: ParagraphStyle):
        reshaped_text = arabic_reshaper.reshape(arabic_text)
        bidirectional_text = algorithm.get_display(reshaped_text)
        formatted_paragraph = Paragraph(bidirectional_text, paragraph_style)
        return formatted_paragraph

arabic_text='وحدة1 وحدة2'

Here's the output:<br>
وحدة2<br>
وحدة1

Here's what I expected:<br>
وحدة1<br>
وحدة2

答案1

得分: 1

Reportlab支持部分双向文本(Bidirectional)功能,使用Fribidi进行支持。默认情况下禁用。在reportlab/rl_settings中有一个名为rtlSupport的选项。请参阅用户指南中的站点配置部分。在我的安装中,我添加了一个名为~/.reportlab_settings的文件,并添加了一行rtlSupport=1

ParagraphStyle()中设置wordWrap="RTL"

示例:

from reportlab.platypus import Paragraph
from reportlab.lib.enums import TA_RIGHT
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import LETTER
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.platypus.doctemplate import SimpleDocTemplate

pdfmetrics.registerFont(TTFont("Arial Unicode", "Arial Unicode.ttf"))

arabic_text='وحدة1 وحدة2'

doc = SimpleDocTemplate(
    "repro_ar.pdf",
    pagesize=LETTER,
    rightMargin=280,
    leftMargin=280,
    topMargin=72,
    bottomMargin=72,
)
styles = getSampleStyleSheet()
normal_arabic = ParagraphStyle(
    parent=styles["Normal"],
    name="NormalArabic",
    wordWrap="RTL",
    alignment=TA_RIGHT,
    fontName="Arial Unicode",
    fontSize=14,
    leading=16
)

flowables = [Paragraph(arabic_text, normal_arabic)]
doc.build(flowables)

这将生成如下图所示的文档:

阿拉伯文本正在报告实验室中反转行。

英文:

Reportlab has partial bidirectional support using Fribidi. It is disabled by default. There is an option rtlSupport in reportlab/rl_settings. See user guide section on site configuration. In my installation, I added a file ~/.reportlab_settings and added the line rtlSupport=1.

In ParagraphStyle() set wordWrap=&quot;RTL&quot;.

An example:

from reportlab.platypus import Paragraph
from reportlab.lib.enums import TA_RIGHT
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import LETTER
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.platypus.doctemplate import SimpleDocTemplate

pdfmetrics.registerFont(TTFont(&quot;Arial Unicode&quot;, &quot;Arial Unicode.ttf&quot;))

arabic_text=&#39;وحدة1 وحدة2&#39;

doc = SimpleDocTemplate(
    &quot;repro_ar.pdf&quot;,
    pagesize=LETTER,
    rightMargin=280,
    leftMargin=280,
    topMargin=72,
    bottomMargin=72,
)
styles = getSampleStyleSheet()
normal_arabic = ParagraphStyle(
    parent=styles[&quot;Normal&quot;],
    name=&quot;NormalArabic&quot;,
    wordWrap=&quot;RTL&quot;,
    alignment=TA_RIGHT,
    fontName=&quot;Arial Unicode&quot;,
    fontSize=14,
    leading=16
)

flowables = [Paragraph(arabic_text, normal_arabic)]
doc.build(flowables)

This gives me:

阿拉伯文本正在报告实验室中反转行。

huangapple
  • 本文由 发表于 2023年7月12日 21:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定