欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)在Word中創(chuàng)建表格并填入數(shù)據(jù)與圖片

 更新時(shí)間:2024年03月11日 10:43:35   作者:E-iceblue  
在Word中,表格是一個(gè)強(qiáng)大的工具,本文主要為大家介紹了如何使用Python在Word中創(chuàng)建表格并填入數(shù)據(jù)、圖片,以及設(shè)置表格樣式等,感興趣的可以了解下

在Word中,表格是一個(gè)強(qiáng)大的工具,它可以幫助你更好地組織、呈現(xiàn)和分析信息。本文將介紹如何使用Python在Word中創(chuàng)建表格并填入數(shù)據(jù)、圖片,以及設(shè)置表格樣式等。

Python Word庫(kù):

要使用Python在Word中創(chuàng)建或操作表格,需要先將Spire.Doc for Python這個(gè)第三方庫(kù)安裝到項(xiàng)目中.

pip install Spire.Doc

使用Python在Word中創(chuàng)建表格并填充數(shù)據(jù)

import math
from spire.doc import *
from spire.doc.common import *
 
# 創(chuàng)建Document對(duì)象
doc = Document()
 
# 添加一節(jié)
section = doc.AddSection()
 
# 創(chuàng)建一個(gè)表格
table = section.AddTable()
 
# 指定表格數(shù)據(jù)
header_data = ["商品名稱", "單位", "數(shù)量", "單價(jià)"]
row_data = [ ["底板-1","件","20946","2.9"], 
                ["定位板-2","張","38931","1.5"], 
                ["整平模具-3","組","32478","1.1"], 
                ["后殼FD1042-4","組","21162","0.6"], 
                ["棍子-5","組","66517","1.2"]]
                
# 設(shè)置表格的行數(shù)和列數(shù)
table.ResetCells(len(row_data) + 1, len(header_data))
 
# 設(shè)置表格自適應(yīng)窗口
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow)
 
# 設(shè)置標(biāo)題行
headerRow = table.Rows[0]
headerRow.IsHeader = True
headerRow.Height = 23
headerRow.RowFormat.BackColor = Color.get_Orange()
 
# 在標(biāo)題行填充數(shù)據(jù)并設(shè)置文本格式
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
 
# 將數(shù)據(jù)填入其余各行并設(shè)置文本格式
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
 
# 設(shè)置交替行顏色
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)

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

輸出結(jié)果:

使用Python在Word表格中插入圖片

from spire.doc import *
from spire.doc.common import *
 
inputFile = "表格示例.docx"
outputFile = "插入圖片到表格.docx"
 
# 創(chuàng)建Document對(duì)象
doc = Document()
 
# 加載Word文檔
doc.LoadFromFile(inputFile)
 
# 獲取文檔中第一個(gè)表格
table = doc.Sections[0].Tables[0]
 
# 將圖片添加到指定單元格并設(shè)置圖片大小
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
 
# 保存結(jié)果文件
doc.SaveToFile(outputFile, FileFormat.Docx)
doc.Close()

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

輸出結(jié)果:

以上就是Python實(shí)現(xiàn)在Word中創(chuàng)建表格并填入數(shù)據(jù)與圖片 的詳細(xì)內(nèi)容,更多關(guān)于Python創(chuàng)建Word表格的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論