<p>使用以下代码。它能很好地包装它。在</p>
<pre><code>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)
</code></pre>