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

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

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:

  1. def format_arabic_paragraph(arabic_text: str, paragraph_style: ParagraphStyle):
  2. reshaped_text = arabic_reshaper.reshape(arabic_text)
  3. bidirectional_text = algorithm.get_display(reshaped_text)
  4. formatted_paragraph = Paragraph(bidirectional_text, paragraph_style)
  5. return formatted_paragraph
  6. 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"

示例:

  1. from reportlab.platypus import Paragraph
  2. from reportlab.lib.enums import TA_RIGHT
  3. from reportlab.pdfbase import pdfmetrics
  4. from reportlab.pdfbase.ttfonts import TTFont
  5. from reportlab.lib.pagesizes import LETTER
  6. from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
  7. from reportlab.platypus.doctemplate import SimpleDocTemplate
  8. pdfmetrics.registerFont(TTFont("Arial Unicode", "Arial Unicode.ttf"))
  9. arabic_text='وحدة1 وحدة2'
  10. doc = SimpleDocTemplate(
  11. "repro_ar.pdf",
  12. pagesize=LETTER,
  13. rightMargin=280,
  14. leftMargin=280,
  15. topMargin=72,
  16. bottomMargin=72,
  17. )
  18. styles = getSampleStyleSheet()
  19. normal_arabic = ParagraphStyle(
  20. parent=styles["Normal"],
  21. name="NormalArabic",
  22. wordWrap="RTL",
  23. alignment=TA_RIGHT,
  24. fontName="Arial Unicode",
  25. fontSize=14,
  26. leading=16
  27. )
  28. flowables = [Paragraph(arabic_text, normal_arabic)]
  29. 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:

  1. from reportlab.platypus import Paragraph
  2. from reportlab.lib.enums import TA_RIGHT
  3. from reportlab.pdfbase import pdfmetrics
  4. from reportlab.pdfbase.ttfonts import TTFont
  5. from reportlab.lib.pagesizes import LETTER
  6. from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
  7. from reportlab.platypus.doctemplate import SimpleDocTemplate
  8. pdfmetrics.registerFont(TTFont(&quot;Arial Unicode&quot;, &quot;Arial Unicode.ttf&quot;))
  9. arabic_text=&#39;وحدة1 وحدة2&#39;
  10. doc = SimpleDocTemplate(
  11. &quot;repro_ar.pdf&quot;,
  12. pagesize=LETTER,
  13. rightMargin=280,
  14. leftMargin=280,
  15. topMargin=72,
  16. bottomMargin=72,
  17. )
  18. styles = getSampleStyleSheet()
  19. normal_arabic = ParagraphStyle(
  20. parent=styles[&quot;Normal&quot;],
  21. name=&quot;NormalArabic&quot;,
  22. wordWrap=&quot;RTL&quot;,
  23. alignment=TA_RIGHT,
  24. fontName=&quot;Arial Unicode&quot;,
  25. fontSize=14,
  26. leading=16
  27. )
  28. flowables = [Paragraph(arabic_text, normal_arabic)]
  29. 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:

确定