溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

發(fā)布時(shí)間:2023-03-29 11:23:00 來(lái)源:億速云 閱讀:130 作者:iii 欄目:開(kāi)發(fā)技術(shù)

今天小編給大家分享一下Python自動(dòng)化辦公之怎么生成PDF報(bào)告的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

因?yàn)楣ぷ餍枰?jīng)常需要生成很多的PDF報(bào)告給客戶查看產(chǎn)品效果以及過(guò)程的講解。

每次都需要按照一定的格式的編寫(xiě)文檔并生成PDF報(bào)告,這樣重復(fù)性的工作實(shí)在太累。

想著可以使用python生成一份給用戶看的報(bào)告,里面需要插入圖片、表格、文字說(shuō)明等等。

使用第三方的python非標(biāo)準(zhǔn)模塊reportlab就能滿足直接生成PDF報(bào)告的需要,由于是非標(biāo)準(zhǔn)庫(kù)需要使用pip的方式安裝一下該模塊。

使用pip安裝reportlab模塊,支持生成PDF文檔。

pip install reportlab -i https://pypi.tuna.tsinghua.edu.cn/simple

若是在安裝過(guò)程中出現(xiàn)缺失C++環(huán)境導(dǎo)致構(gòu)建失敗時(shí),可以直接選擇使用wheel文件的方式安裝reportlab模塊。

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

.whl文件的下載地址如下:https://www.lfd.uci.edu/~gohlke/pythonlibs/

下載完成之后存儲(chǔ)到本地磁盤(pán),按照存放的路徑安裝reportLab模塊即可,安裝方式可以參考下面的安裝方式。

pip install wheel -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install D:\downloads\reportlab-3.5.57-cp36-cp36m-win_amd64.whl

提前將reportlab模塊中需要使用到的python對(duì)象導(dǎo)入到當(dāng)前的代碼塊中。

from reportlab.pdfbase import pdfmetrics  # 注冊(cè)字體
from reportlab.pdfbase.ttfonts import TTFont  # 字體類
from reportlab.platypus import Table, SimpleDocTemplate, Paragraph, Image  # 報(bào)告內(nèi)容相關(guān)類
from reportlab.lib.pagesizes import letter  # 頁(yè)面的標(biāo)志尺寸(8.5*inch, 11*inch)
from reportlab.lib.styles import getSampleStyleSheet  # 文本樣式
from reportlab.lib import colors  # 顏色模塊
from reportlab.lib.units import cm  # 單位:cm

模塊導(dǎo)入完成之后,第一步需要設(shè)置PDF文檔中使用到的字體,字體可以根據(jù)自己的喜好自行設(shè)置。

# Registering a font named 'simfang' with the file 'simfang.ttf'.
pdfmetrics.registerFont(TTFont('simfang', 'simfang.ttf'))

我這里選擇的字體是simfang.ttf,關(guān)于windows系統(tǒng)中的默認(rèn)字體可以下面的路徑中查看。

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

開(kāi)發(fā)業(yè)務(wù)代碼之前,我們可以將公共的部分提到最外面,這里使用getSampleStyleSheet函數(shù)將獲取到所有的樣式表后面在其他地方也可以使用。

# Getting a list of styles that can be used in the document.
style_list = getSampleStyleSheet()

1、插入PDF標(biāo)題

大標(biāo)題設(shè)置字體樣式對(duì)象為Heading1,字體顏色為綠色,大小為18并且加粗。

def insert_full_title(title_name=None):
    """
    This function takes in a title name and returns the full title name.

    :param title_name: The name of the title you want to insert
    """
    font_ = style_list['Heading1']
    font_.fontName = 'simfang'
    font_.fontSize = 18
    font_.leading = 50
    font_.textColor = colors.green
    font_.alignment = 1
    font_.bold = True
    return Paragraph(title_name, font_)

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

2、插入PDF小標(biāo)題

小標(biāo)題設(shè)置字體樣式對(duì)象為Normal,字體顏色為紅色,大小為15并且不加粗。

def insert_lettle_title(lettle_name=None):
    """
    :param lettle_name: The name of the lettle you want to insert
    """
    font_ = style_list['Normal']
    font_.fontName = 'simfang'
    font_.fontSize = 15
    font_.leading = 30
    font_.textColor = colors.red
    return Paragraph(lettle_name, font_)

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

3、插入普通段落文本

普通文本設(shè)置字體樣式對(duì)象為Normal,字體顏色為默認(rèn),大小為12并且不加粗,開(kāi)啟自動(dòng)換行模式。

def insert_text(text=None):
    """
    > This function inserts text into the current document

    :param text: The text to insert
    """
    font_ = style_list['Normal']
    font_.fontName = 'simfang'
    font_.fontSize = 12
    font_.wordWrap = 'CJK'
    font_.alignment = 0
    font_.firstLineIndent = 32
    font_.leading = 25
    return Paragraph(text, font_)

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

4、插入PDF圖片

將圖片插入到PDF文檔對(duì)象中比較簡(jiǎn)單,只需要設(shè)置需要插入圖片的本地路徑即可。

def insert_image(image_path=None):
    """
    > This function inserts an image into the notebook

    :param image_path: The path to the image you want to insert
    """
    img = Image(image_path)
    img.drawWidth = 5 * cm
    img.drawHeight = 8 * cm
    return img

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

5、插入PDF表格

插入表格時(shí),表格的格式可以根據(jù)自己的喜好設(shè)置表格的標(biāo)題、字體樣式、字體大小以及是否需要合并等參數(shù)來(lái)控制需要插入的表格對(duì)象。

def insert_table(*args):
    """
    It inserts a table into the database.
    """
    col_width = 120
    style = [
        ('FONTNAME', (0, 0), (-1, -1), 'simfang'),  # 字體
        ('FONTSIZE', (0, 0), (-1, 0), 12),  # 第一行的字體大小
        ('FONTSIZE', (0, 1), (-1, -1), 10),  # 第二行到最后一行的字體大小
        ('BACKGROUND', (0, 0), (-1, 0), '#d5dae6'),  # 設(shè)置第一行背景顏色
        ('ALIGN', (0, 0), (-1, -1), 'CENTER'),  # 第一行水平居中
        ('ALIGN', (0, 1), (-1, -1), 'LEFT'),  # 第二行到最后一行左右左對(duì)齊
        ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),  # 所有表格上下居中對(duì)齊
        ('TEXTCOLOR', (0, 0), (-1, -1), colors.darkslategray),  # 設(shè)置表格內(nèi)文字顏色
        ('GRID', (0, 0), (-1, -1), 0.5, colors.grey),  # 設(shè)置表格框線為grey色,線寬為0.5
    ]
    table = Table(args, colWidths=col_width, style=style)
    return table

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

上述就是PDF文檔中常用的對(duì)象,最后通過(guò)添加對(duì)應(yīng)的內(nèi)容參數(shù)即可生成PDF文檔并保存到本地磁盤(pán)當(dāng)中。

# A special variable in Python that evaluates to `True` if the module is being run as the main program.
if __name__ == '__main__':
    pdf_ = list()

    pdf_.append(insert_full_title('數(shù)據(jù)測(cè)試報(bào)告'))
    pdf_.append(insert_text(
        'Python 是一門(mén)編程語(yǔ)言。 您可以在服務(wù)器上使用 Python 來(lái)創(chuàng)建 Web 應(yīng)用程序。通過(guò)實(shí)例學(xué)習(xí) 我們的 TIY 編輯器使學(xué)習(xí) Python 變得簡(jiǎn)單,它能夠同時(shí)顯示代碼和結(jié)果。 '))
    pdf_.append(insert_image('./excle源數(shù)據(jù).png'))
    pdf_.append(insert_lettle_title('數(shù)據(jù)內(nèi)容展示:'))
    data = [
        ('職位名稱', '平均薪資', '較上年增長(zhǎng)率'),
        ('數(shù)據(jù)分析師', '18.5K', '25%'),
        ('高級(jí)數(shù)據(jù)分析師', '25.5K', '14%'),
        ('資深數(shù)據(jù)分析師', '29.3K', '10%')
    ]
    pdf_.append(insert_table(*data))

    doc = SimpleDocTemplate('測(cè)試報(bào)告.pdf', pagesize=letter)
    doc.build(pdf_)

Python自動(dòng)化辦公之怎么生成PDF報(bào)告

以上就是“Python自動(dòng)化辦公之怎么生成PDF報(bào)告”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI