英文:
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="RTL"
.
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("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)
This gives me:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论