Python 在Word中創建表格並填入數據、圖片

在Word中,表格是一個強大的工具,它可以幫助你更好地組織、呈現和分析信息。本文將介紹如何使用Python在Word中創建表格並填入數據、圖片,以及設置表格樣式等。

Python Word庫:

要使用Python在Word中創建或操作表格,需要先將Spire.Doc for Python這個第三方庫安裝到項目中.

pip install Spire.Doc

 

示例代碼1:使用Python在Word中創建表格並填充數據

import math
from spire.doc import *
from spire.doc.common import *
 
# 創建Document對象
doc = Document()
 
# 添加一節
section = doc.AddSection()
 
# 創建一個表格
table = section.AddTable()
 
# 指定表格數據
header_data = ["商品名稱", "單位", "數量", "單價"]
row_data = [ ["底板-1","","20946","2.9"], 
                ["定位板-2","","38931","1.5"], 
                ["整平模具-3","","32478","1.1"], 
                ["後殼FD1042-4","","21162","0.6"], 
                ["棍子-5","","66517","1.2"]]
                
# 設置表格的行數和列數
table.ResetCells(len(row_data) + 1, len(header_data))
 
# 設置表格自適應窗口
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow)
 
# 設置標題行
headerRow = table.Rows[0]
headerRow.IsHeader = True
headerRow.Height = 23
headerRow.RowFormat.BackColor = Color.get_Orange()
 
# 在標題行填充數據並設置文本格式
i = 0
while i < len(header_data):
    headerRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle
    paragraph = headerRow.Cells[i].AddParagraph()
    paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
    txtRange = paragraph.AppendText(header_data[i])
    txtRange.CharacterFormat.Bold = True
    txtRange.CharacterFormat.FontSize = 12
    i += 1
 
# 將數據填入其餘各行並設置文本格式
r = 0
while r < len(row_data):
    dataRow = table.Rows[r + 1]
    dataRow.Height = 20
    dataRow.HeightType = TableRowHeightType.Exactly
    c = 0
    while c < len(row_data[r]):
        dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle
        paragraph = dataRow.Cells[c].AddParagraph()
        paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
        txtRange =  paragraph.AppendText(row_data[r][c])
        txtRange.CharacterFormat.FontSize = 11
        c += 1
    r += 1
 
# 設置交替行顏色
for j in range(1, table.Rows.Count):
    if math.fmod(j, 2) == 0:
        row2 = table.Rows[j]
        for f in range(row2.Cells.Count):
            row2.Cells[f].CellFormat.BackColor = Color.get_LightGray()
 
# 保存文件
doc.SaveToFile("Word表格.docx", FileFormat.Docx2016)

以下示例通過Section.AddTable() 方法在Word文檔中添加了一個表格,然後將列表中的數據填充到了指定的單元格。此外Spire.Doc for Python庫還提供了接口設置單元格樣式等。

輸出結果:

 

代碼示例2:使用Python在Word表格中插入圖片

from spire.doc import *
from spire.doc.common import *
 
inputFile = "表格示例.docx"
outputFile = "插入圖片到表格.docx"
 
# 創建Document對象
doc = Document()
 
# 加載Word文檔
doc.LoadFromFile(inputFile)
 
# 獲取文檔中第一個表格
table = doc.Sections[0].Tables[0]
 
# 將圖片添加到指定單元格並設置圖片大小
cell = table.Rows[1].Cells[1]
picture = cell.Paragraphs[0].AppendPicture("python.png")
picture.Width = 80
picture.Height = 80
 
cell = table.Rows[2].Cells[1]
picture = cell.Paragraphs[0].AppendPicture("java.jpg")
picture.Width = 80
picture.Height = 80
 
# 保存結果文件
doc.SaveToFile(outputFile, FileFormat.Docx)
doc.Close()
 

從以上代碼可以看出,要在Word表格中插入圖片,需要先獲取指定的單元格,然後使用TableCell.Paragraphs[index].AppendPicture() 方法插入圖片。

輸出結果:


 

Spire.Doc for Python庫還支持對Word中的表格進行其他操作,如添加、刪除複製行或列、合併或拆分單元格等。更多示例demo可查看:

https://www.e-iceblue.cn/docforpython/spire-doc-for-python-program-guide-content.html

對於水印問題,可以點擊申請臨時授權移除,或者發送郵件到[email protected]

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