使用reportlab python模块将泰米尔字符写入PDF?

2024-04-26 07:50:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个UTF-8编码的文件,内容是泰米尔语(一种印度语)。我必须阅读文件的内容并制作一个PDF。我使用reportlabpython模块来完成这项工作。在

我可以打开文件,阅读内容,并打印到终端显示完美的内容。但是,在使用reportlab将内容写入PDF时,某些字符(由两个“字符符号”组合而成)的顺序在复合字符中颠倒。我为reportlab段落样式设置了泰米尔语字体。我错过了什么?在

from reportlab.pdfbase import pdfmetrics
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Latha', '/home/srinivas/Fonts/latha/latha.ttf'))
from os import listdir
from os.path import isdir, isfile, join
import random
import codecs
from tamil import utf8 as tamil
PATH = 'tamil_file'
num_sets = 1
pages_per_set = 12
num_articles_per_page = 2

styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='CustomPara', fontName='Mangal', fontSize=14, alignment=TA_JUSTIFY, leading=24))

style = styles['CustomPara']
styleH = styles['Heading1']

for set_idx in range(num_sets):
    doc = SimpleDocTemplate(str(set_idx)+'.pdf', pagesize=A4)
    story = []
    for page in range(pages_per_set):
            story.append(Spacer(1, 0.1* inch))
            story.append(Paragraph(id, styleH))
            story.append(Spacer(1,  0.1 * inch))
            with codecs.open(join(PATH,selected_file),'r','utf-8') as f:
                for l in f.readlines():
                    print l # prints correctly in terminal
                    lines += l
            story.append(Paragraph(lines, style))
        story.append(PageBreak())
    doc.build(story)

实际文本:நாவல் மரத்தின் மருத்துவப் பயன்கள் போற்றத்தக்கவை

保存错误的文本:enter image description here

注意:如果我从PDF中复制文本并粘贴到此处,则显示良好(错误的文本是图像附件)!


Tags: 文件infrom文本import内容pdflib