1.环境搭建-
**Python2.7.11**
下载地址:https://www.python.org/downloads/
注意:最好不要使用Python 3以上的版本,目前很多库对3以上的版本支持不是很好
**preppy**
安装方式:Python -m pip install preppy 默认是下载最新版本 (指定版本:Python -m pip install preppy2.3.2)
源码下载地址:https://pypi.python.org/pypi/preppy/
**pyrxp**
安装方式:Python -m pip install pyrxp 默认是下载最新版本 (指定版本:Python -m pip install pyRXP-2.1.0)
源码下载地址:https://pypi.python.org/pypi/pyRXP 注意选择对应python版本下载
**reportlab**
安装方式:Python -m pip install reportlab 默认是下载最新版本 (指定版本:Python -m pip install reportlab-3.2.0)
源码下载地址:https://pypi.python.org/pypi/reportlab/
**trml2pdf**
安装方式:Python -m pip install trml2pdf 默认是下载最新版本 (指定版本:Python -m pip install trml2pdf-3.2.0)
源码下载地址:https://pypi.python.org/pypi/trml2pdf
2 、测试RML文档转换为
繁琐的环境搭建完成后就要写RML转换为pdf的工具类了。。。。。下班了回家在写。 2016.2.3
编写pdf工具类:
<span style="background-color: rgb(255, 255, 153);"><span style="color:#ff0000;font-size:24px;"># coding=gbk</span><span style="color:#343434;font-size:24px;"> </span><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace;"><span style="font-size:14px;">#如果代码中有中文字符需加入这段代码</span></span></span><span style="color:#343434;"><span style="font-size:24px;">
</span><span style="font-size:18px;">import sys
import os
import unittest
import trml2pdf # dev mode: python setup.py develop
from six import text_type</span></span><span style="color:#343434;font-size:24px;">
</span>
<span style="color:#343434;"></span><pre name="code" class="python" style="font-size:24px;"><span style="color: rgb(52, 52, 52);font-size:24px; background-color: rgb(255, 255, 153);">#加入这段代码主要是用py2exe制作exe时py2exe无法找到rl_settings</span>
from reportlab import rl_settings
<span style="font-size:18px;color:#343434;">import reportlab.lib.styles</span>
<span style="font-size:18px;color:#343434;">
</span>
<span style="font-size:18px;color:#343434;"><span style="background-color: rgb(255, 255, 153);">#以下两个包用来注册字体,应为我们文档需要显示中文,所以需要注册中文字体
from reportlab.pdfbase import pdfmetrics,ttfonts
from reportlab.lib.fonts import addMapping</span> </span><span style="color:#343434;font-size:24px;">
</span><span style="font-size:18px;color:#343434;">EXAMPLES_DIR = "..\examples"</span>
<span style="color:#343434;font-size:24px;">#</span><span style="font-size:14px;"><span style="color:#343434;">用</span><span style="color: rgb(52, 52, 52); font-family: 'Source Code Pro', monospace;">os.path.realpath(os.path.dirname(os.path.abspath(sys.argv[0])))替代__file__ py2exe不认识__file__</span></span><span style="color:#343434;"><span style="font-size:24px;">
</span><span style="font-size:18px;">TESTS_DIR = os.path.dirname(os.path.realpath(os.path.dirname(os.path.abspath(sys.argv[0]))))
FONT_DIR = "\templates\font"
# sys.path.append(EXAMPLES_DIR)
class TestExamples(unittest.TestCase):
"""run pdf genration using all files in examples."""
def test_run_all(self):
try:
# change current dir, there are relative references to images in rmls
work_dir = os.getcwd()
os.chdir(EXAMPLES_DIR)
<span style="background-color: rgb(255, 255, 153);">pdfmetrics.registerFont(ttfonts.TTFont('song', "C:\Windows\Fonts\simsun.ttc")) #注册字体的方法</span>
self._run_all_examples()
finally:
os.chdir(work_dir)
def _run_all_examples(self):
for name in os.listdir('.'):
if name.endswith(".rml"):
path = name # '{}/{}'.format(EXAMPLES_DIR, name)
print('running: {}'.format(path))
with open(path, "r") as f:
output = trml2pdf.parseString(f.read())
self.assertIsNotNone(output)
with open(TESTS_DIR + '/templates/bug8.pdf', 'wb') as w:
w.write(output)
# does not work, todo find a solution
# class TestOutput(unittest.TestCase):
# def test_bug8(self):
# with open(TESTS_DIR + '/templates/bug_8.rml', "r") as f:
# output = trml2pdf.parseString(f.read())
# print(output)
# self.assertNotIn('\\n', text_type(output)) # not so easy to test PDF content, it is encoded
# with open(TESTS_DIR + '/templates/bug8.pdf', 'wb') as w:
# w.write(output)
if __name__ == "__main__":
unittest.main()</span></span>
代码
中的路径,自己改成合适的就OK了。。。
工具类写完后,用现有的RML做测试,运行测试OK。。。
把脚本打包成exe可执行程序的方法有两种,一种是利用Py2exe,一种是利用PyInstaller
使用方法找度娘就是了。。。
Python各种依赖包地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/