เราจะมาทดลองสร้าง Report แบบ
PDF จาก
RML กันนะครับ
1.ทำการสร้างไฟล์ XML ขึ้นมาก่อนโดนใส่โค้ดง่ายๆ เช่น
โค้ด: เลือกทั้งหมด
<!DOCTYPE document SYSTEM "rml.dtd">
<document filename="example1.pdf">
<template>
<!--this section contains elements of the document -->
<!--which are FIXED into position. -->
<pageTemplate id="main">
<frame id="first" x1="100" y1="400" width="150" height="200"/>
</pageTemplate>
</template>
<stylesheet>
</stylesheet>
<story>
<para>
Welcome to RML!
</para>
</story>
</document>
2.จากนั้น save ไฟล์ XML แล้วเก็บไว้ที่ path ของโปรเจคดังภาพ
- Selection_759.png (56.98 KiB) Viewed 1069 times
3.สร้างไฟล์ Python แล้ว save เก็บไว้ใน path เดียวกันกับ XML เพื่อเรียกใช้งาน XML โดยใช้โค้ด
**อย่าลืม import from z3c.rml import rml2pdf ด้วยละ
โค้ด: เลือกทั้งหมด
# -*- coding: utf-8 -*-
from z3c.rml import rml2pdf
with open('example.rml') as rml:
data = rml.read()
pdf = rml2pdf.parseString(data.decode('utf-8'))
with open('test.pdf', 'w') as output:
output.write(pdf.read())
4.จากนั้นกดรัน จะเห็นได้ว่า Python ทำการสร้างไฟล์ Report ขึ้นมา 1 ไฟล์ชื่อว่า test.pdf ดังภาพ
- Selection_760.png (48.94 KiB) Viewed 1069 times
5.เมื่อกดเปิดดูไฟล์ test.pdf ที่สร้างมาจะเห็นข้อความที่เราสร้างไว้ใน XML ดังภาพ
- Selection_761.png (2.95 KiB) Viewed 1069 times
เป็นไงกันบ้างครับสำหรับการสร้างไฟล์ Report จาก python และ xml สำหรับวิธีนี้สามารถนำไปประยุกต์ในช้การเขียนโปรแกรมเพื่อรายงานต่างๆได้
ศึกษาข้อมูลเพิ่มเติมได้ที่
บทเรียน Python
เทคนิคการเขียน Python
วีดีโอสอนเขียน Python สำหรับพื้นฐาน
Python Knowledge
Programming - C/C++ & java & Python[/size]
เราจะมาทดลองสร้าง Report แบบ [url=https://mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2014-pdf-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]PDF[/url] จาก [url=https://www.mindphp.com/forums/viewtopic.php?f=140&t=29830]RML[/url] กันนะครับ
1.ทำการสร้างไฟล์ XML ขึ้นมาก่อนโดนใส่โค้ดง่ายๆ เช่น
[code]<!DOCTYPE document SYSTEM "rml.dtd">
<document filename="example1.pdf">
<template>
<!--this section contains elements of the document -->
<!--which are FIXED into position. -->
<pageTemplate id="main">
<frame id="first" x1="100" y1="400" width="150" height="200"/>
</pageTemplate>
</template>
<stylesheet>
</stylesheet>
<story>
<para>
Welcome to RML!
</para>
</story>
</document>
[/code]
2.จากนั้น save ไฟล์ XML แล้วเก็บไว้ที่ path ของโปรเจคดังภาพ
[attachment=2]Selection_759.png[/attachment]
3.สร้างไฟล์ Python แล้ว save เก็บไว้ใน path เดียวกันกับ XML เพื่อเรียกใช้งาน XML โดยใช้โค้ด [color=#FF0000]**อย่าลืม import from z3c.rml import rml2pdf ด้วยละ[/color]
[code]# -*- coding: utf-8 -*-
from z3c.rml import rml2pdf
with open('example.rml') as rml:
data = rml.read()
pdf = rml2pdf.parseString(data.decode('utf-8'))
with open('test.pdf', 'w') as output:
output.write(pdf.read())
[/code]
4.จากนั้นกดรัน จะเห็นได้ว่า Python ทำการสร้างไฟล์ Report ขึ้นมา 1 ไฟล์ชื่อว่า test.pdf ดังภาพ
[attachment=1]Selection_760.png[/attachment]
5.เมื่อกดเปิดดูไฟล์ test.pdf ที่สร้างมาจะเห็นข้อความที่เราสร้างไว้ใน XML ดังภาพ
[attachment=0]Selection_761.png[/attachment]
เป็นไงกันบ้างครับสำหรับการสร้างไฟล์ Report จาก python และ xml สำหรับวิธีนี้สามารถนำไปประยุกต์ในช้การเขียนโปรแกรมเพื่อรายงานต่างๆได้
ศึกษาข้อมูลเพิ่มเติมได้ที่
[url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python.html]บทเรียน Python[/url]
[url=https://www.mindphp.com/developer/tips-python.html]เทคนิคการเขียน Python[/url]
[url=https://www.mindphp.com/vdo-tutorial-python.html]วีดีโอสอนเขียน Python สำหรับพื้นฐาน[/url]
[url=https://www.mindphp.com/forums/viewforum.php?f=144]Python Knowledge[/url]
[url=https://www.mindphp.com/forums/viewforum.php?f=16]Programming - C/C++ & java & Python[/url][/size]