Python實(shí)現(xiàn)在Word中創(chuàng)建表格并填入數(shù)據(jù)與圖片
在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 = ["商品名稱(chēng)", "單位", "數(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)文章
使用Python實(shí)現(xiàn)分組數(shù)據(jù)并保存到單獨(dú)的文件中
當(dāng)處理大型數(shù)據(jù)集時(shí),通常需要將數(shù)據(jù)分組,并將每個(gè)分組的數(shù)據(jù)保存到單獨(dú)的文件中,本文將使用 Python 中的 pandas 庫(kù)來(lái)實(shí)現(xiàn)這一目標(biāo),需要的可以參考下2024-04-04
python 利用pywifi模塊實(shí)現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實(shí)時(shí)監(jiān)控網(wǎng)絡(luò)
這篇文章主要介紹了python 利用pywifi模塊實(shí)現(xiàn)連接網(wǎng)絡(luò)破解wifi密碼實(shí)時(shí)監(jiān)控網(wǎng)絡(luò),需要的朋友可以參考下2019-09-09
python神經(jīng)網(wǎng)絡(luò)slim常用函數(shù)訓(xùn)練保存模型
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)使用slim函數(shù)進(jìn)行模型的訓(xùn)練及保存模型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
python數(shù)據(jù)分析之單因素分析線性擬合及地理編碼
這篇文章主要介紹了python數(shù)據(jù)分析之單因素分析線性擬合及地理編碼,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
python讀取與寫(xiě)入tif圖片的完整信息(過(guò)程詳解)
這篇文章主要介紹了python讀取與寫(xiě)入tif圖片的完整信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
用pip給python安裝matplotlib庫(kù)的詳細(xì)教程
這篇文章主要介紹了用pip給python安裝matplotlib庫(kù)的詳細(xì)教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
pygame.display.flip()和pygame.display.update()的區(qū)別及說(shuō)明
這篇文章主要介紹了pygame.display.flip()和pygame.display.update()的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Python連接MySQL并使用fetchall()方法過(guò)濾特殊字符
這篇文章主要介紹了Python連接MySQL的方法并講解了如何使用fetchall()方法過(guò)濾特殊字符,示例環(huán)境為Ubuntu操作系統(tǒng),需要的朋友可以參考下2016-03-03

