Python Generate tables in PDF(2)

接着上一篇博客繼續實現PDF中表格的繪製:https://blog.csdn.net/m0_37690102/article/details/107051058

直接上代碼:在上一篇博客中,您已經能夠簡單的能在PDF中生成相應的表格了,但是還是存在一點點小問題,對於表格內部的文字信息沒辦法直接寫入,已經表格不屬於連通區域的。

  • Code directly: In the last blog post, you were able to easily generate tables in PDF, but there is still a small problem. The text information inside the table cannot be written directly, and the table does not belong to the connected area.

完整的代碼您可以參考下面我給出的,如果存在相應的問題,請您及時和我聯繫,非常感謝您的反饋。

  • You can refer to the complete code below. If there is any problem, please contact me in time. Thank you very much for your feedback.
# encoding:utf-8
# -*- coding: utf-8 -*-
#字體庫
import reportlab.lib.fonts
#canvas畫圖的類庫
from reportlab.pdfgen.canvas import Canvas
#用於定位的inch庫,inch將作爲我們的高度寬度的單位
from reportlab.lib.units import inch

def create_imaginarylinetable(canvas,row,col,start_y1,start_x1,data):#虛線表格
    for i in range(row):
        canvas.line((start_x1) * inch, (start_y1-0.5*i) * inch, 7 * inch, (start_y1-0.5*i) * inch)
        for j in range(col):
            #1   2.2    3.4   4.6  5.8   7
            canvas.line((start_x1+j*1.2) * inch, (start_y1-0.5*i) * inch,
                        (start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1) * inch)#5.9
            canvas.line((start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05) * inch,
                        (start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05-0.2)* inch)#5.65
            canvas.line((start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05-0.2-0.05) * inch,
                        (start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05-0.2-0.05-0.1) * inch)#5.5
    canvas.line((start_x1) * inch, (start_y1 - 0.5 * row) * inch, 7 * inch, (start_y1 - 0.5 * row) * inch)
    #寫入數據
    for i in range(row):
        for j in range(col-1):
            # print(str(data[i][j]))
            canvas.drawString((start_x1+j*1.2+0.45) * inch, (start_y1-0.5*i-0.3) * inch, str(data[i][j]))
    return   (start_y1 - 0.5 * row)

def create_Dislocationtable(canvas,row,col,start_y1,start_x1,offset,data):#錯位表格
    for i in range(row):
        canvas.line((start_x1) * inch, (start_y1-0.5*i) * inch, 7 * inch, (start_y1-0.5*i) * inch)
        for j in range(col):
            #1   2.2    3.4   4.6  5.8   7
            if j==0:
                canvas.line((start_x1+j*1.2) * inch, (start_y1-0.5*i) * inch,
                            (start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1) * inch)#5.9
                canvas.line((start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05) * inch,
                            (start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05-0.2)* inch)#5.65
                canvas.line((start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05-0.2-0.05) * inch,
                            (start_x1+j*1.2) * inch, (start_y1-0.5*i-0.1-0.05-0.2-0.05-0.1) * inch)#5.5
            else:
                canvas.line((start_x1 + j * 1.2) * inch, (start_y1 - 0.5 * i) * inch,
                            (start_x1 + j * 1.2) * inch, (start_y1 - 0.5 * i - 0.1) * inch)  # 5.9
                canvas.line((start_x1 + j * 1.2-offset) * inch, (start_y1 - 0.5 * i - 0.1 - 0.05) * inch,
                            (start_x1 + j * 1.2-offset) * inch, (start_y1 - 0.5 * i - 0.1 - 0.05 - 0.2) * inch)  # 5.65
                canvas.line((start_x1 + j * 1.2) * inch, (start_y1 - 0.5 * i - 0.1 - 0.05 - 0.2 - 0.05) * inch,
                            (start_x1 + j * 1.2) * inch,
                            (start_y1 - 0.5 * i - 0.1 - 0.05 - 0.2 - 0.05 - 0.1) * inch)  # 5.5
    canvas.line((start_x1) * inch, (start_y1 - 0.5 * row) * inch, 7 * inch, (start_y1 - 0.5 * row) * inch)
    #寫入數據
    for i in range(row):
        for j in range(col-1):
            # print(str(data[i][j]))
            canvas.drawString((start_x1+j*1.2+0.45) * inch, (start_y1-0.5*i-0.3) * inch, str(data[i][j]))
    return   (start_y1 - 0.5 * row)
def pdf_head(canvas, data):
    #setFont是字體設置的函數,第一個參數是類型,第二個是大小
    canvas.setFont("Helvetica-Bold", 11.5)
    #---------------------------------------------------------------
    # draw a line segment from (x1,y1) to (x2,y2)
    # ---------------------------------------------------------------
    # create_table(canvas,row,col,start_y1,start_x1):
    data = [("@@E1", "##E2", "%%E3", "$$E4", "result"),
            ('AB', "100", "80", "30", '35'),
            ('AC', '25', '60', '55', '45'),
            ('AD', '30', '90', '75', '80'),
            ('AE', '00', '44', '88', '11'),
            ('AF', '11', '22', '33', '44')]
    NEXT_START = create_imaginarylinetable(canvas, 6,6,11,1,data)#6行5列的表格
    data = [("@@E1", "##E2", "%%E3", "$$E4", "result"),
            ('AB', "100", "80", "30", '35'),
            ('AC', '25', '60', '55', '45'),
            ('AD', '30', '90', '75', '80'),
            ('AE', '00', '44', '88', '11'),
            ('AF', '11', '22', '33', '44')]
    NEXT_START = create_Dislocationtable(canvas, 6, 6, NEXT_START-1, 1,0.1,data)  # 5行5列的表格  offset=0.1

    data = [("@@E1", "##E2", "%%E3", "$$E4", "result"),
            ('AB', "100", "80", "30", '35'),
            ('AC', '25', '60', '55', '45'),
            ('AD', '30', '90', '75', '80'),
            ('AE', '00', '44', '88', '11'),
            ('AF', '11', '22', '33', '44')]
    create_Dislocationtable(canvas, 6, 6, NEXT_START-0.75, 1,0.1,data)  # 5行5列的表格  offset=0.1

if __name__ == "__main__":
    #聲明Canvas類對象,傳入的就是要生成的pdf文件名字
    can = Canvas('report.pdf')
    data = ""
    pdf_head(can, data)
    #showpage將保留之前的操作內容之後新建一張空白頁
    can.showPage()
    #將所有的頁內容存到打開的pdf文件裏面。
    can.save()

看看今天的效果如何,是否滿意。

  • Let's see how it works today and if we're satisfied.

 I hope I can help you,If you have any questions, please  comment on this blog or send me a private message. I will reply in my free time. 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章