Python實(shí)現(xiàn)在Word中創(chuàng)建表格并填入數(shù)據(jù)與圖片
在Word中,表格是一個(gè)強(qiáng)大的工具,它可以幫助你更好地組織、呈現(xiàn)和分析信息。本文將介紹如何使用Python在Word中創(chuàng)建表格并填入數(shù)據(jù)、圖片,以及設(shè)置表格樣式等。
Python Word庫:
要使用Python在Word中創(chuàng)建或操作表格,需要先將Spire.Doc for Python這個(gè)第三方庫安裝到項(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)以下示例通過Section.AddTable() 方法在Word文檔中添加了一個(gè)表格,然后將列表中的數(shù)據(jù)填充到了指定的單元格。此外Spire.Doc for Python庫還提供了接口設(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)文章
Python3使用正則表達(dá)式爬取內(nèi)涵段子示例
這篇文章主要介紹了Python3使用正則表達(dá)式爬取內(nèi)涵段子,涉及Python正則匹配與文件讀寫相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
python數(shù)據(jù)化運(yùn)營(yíng)的重要意義
在本篇文章里小編給大家分享的是關(guān)于python數(shù)據(jù)化運(yùn)營(yíng)的重要意義,有興趣的朋友們可以學(xué)習(xí)下。2019-11-11
Flask如何結(jié)合Jinja2模板引擎返回渲染后HTML
在 Flask 中結(jié)合 Jinja2 模板引擎返回渲染后的 HTML 是核心功能之一,本文主要介紹了詳細(xì)實(shí)現(xiàn)方法和最佳實(shí)踐,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06
Python報(bào)錯(cuò)NameError: name ‘secrets‘ is not
在使用Python進(jìn)行安全編程時(shí),我們經(jīng)常需要使用secrets模塊來生成安全的隨機(jī)數(shù),但是卻遇到這個(gè)問題,本文主要介紹了Python報(bào)錯(cuò)NameError: name ‘secrets‘ is not defined解決,感興趣的可以了解一下2024-06-06
python導(dǎo)入三方包的顯示未解析的引用但是可以運(yùn)行問題解決
如果 PyCharm 顯示未解析的引用,有可能是因?yàn)槟诖a中使用了未安裝的庫或者模塊,或者是因?yàn)?nbsp;PyCharm 沒有正確配置解釋器,這篇文章主要介紹了python導(dǎo)入三方包的顯示未解析的引用但是可以運(yùn)行問題的解決辦法2025-12-12
基于Python繪制美觀動(dòng)態(tài)圓環(huán)圖、餅圖
這篇文章主要介紹了基于Python制作美觀動(dòng)態(tài)圓環(huán)圖、餅圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

