Created
November 15, 2015 05:02
-
-
Save ssddi456/caf14b62ea180b6a6b28 to your computer and use it in GitHub Desktop.
trans chinese txt to pdf with reportlab, so i can read it on dpt-s1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- 1 | |
import re | |
import sys | |
from reportlab.pdfbase import pdfmetrics | |
from reportlab.pdfbase.ttfonts import TTFont | |
from reportlab.lib.styles import ParagraphStyle | |
from reportlab.platypus import Paragraph, SimpleDocTemplate, PageBreak | |
pdfmetrics.registerFont(TTFont('msyh', 'C:/Windows/Fonts/msyh.ttf')) | |
pdfmetrics.registerFont(TTFont('msyhBd', 'C:/Windows/Fonts/msyh.ttf')) | |
pdfmetrics.registerFont(TTFont('msyhIt', 'C:/Windows/Fonts/msyh.ttf')) | |
pdfmetrics.registerFont(TTFont('msyhBI', 'C:/Windows/Fonts/msyh.ttf')) | |
style = ParagraphStyle('test') | |
style.fontName = 'msyh' | |
style.fontSize = 10 | |
style.firstLineIndent=2*style.fontSize | |
style.leading = 1.4*style.fontSize | |
spaceBefore = 0.5*style.fontSize | |
def main(): | |
try: | |
in_file = sys.argv[1] | |
out_file = sys.argv[2] | |
print in_file | |
print out_file | |
f = open(in_file,'r') | |
except Exception, e: | |
print e | |
return | |
story = [] | |
for line in f.readlines(): | |
line = line.decode('utf-8') | |
line = re.sub(r"</?[a-zA-z]>?", "", line) | |
p = Paragraph(line, style) | |
p.spaceBefore = spaceBefore | |
story.append(p) | |
doc = SimpleDocTemplate(out_file) | |
doc.build(story) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment