Python實現在Word中創(chuàng)建表格并填入數據與圖片
在Word中,表格是一個強大的工具,它可以幫助你更好地組織、呈現和分析信息。本文將介紹如何使用Python在Word中創(chuàng)建表格并填入數據、圖片,以及設置表格樣式等。
Python Word庫:
要使用Python在Word中創(chuàng)建或操作表格,需要先將Spire.Doc for Python這個第三方庫安裝到項目中.
pip install Spire.Doc
使用Python在Word中創(chuàng)建表格并填充數據
import math
from spire.doc import *
from spire.doc.common import *
# 創(chuàng)建Document對象
doc = Document()
# 添加一節(jié)
section = doc.AddSection()
# 創(chuàng)建一個表格
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庫還提供了接口設置單元格樣式等。
輸出結果:

使用Python在Word表格中插入圖片
from spire.doc import *
from spire.doc.common import *
inputFile = "表格示例.docx"
outputFile = "插入圖片到表格.docx"
# 創(chuàng)建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() 方法插入圖片。
輸出結果:

以上就是Python實現在Word中創(chuàng)建表格并填入數據與圖片 的詳細內容,更多關于Python創(chuàng)建Word表格的資料請關注腳本之家其它相關文章!
相關文章
python 利用pywifi模塊實現連接網絡破解wifi密碼實時監(jiān)控網絡
這篇文章主要介紹了python 利用pywifi模塊實現連接網絡破解wifi密碼實時監(jiān)控網絡,需要的朋友可以參考下2019-09-09
pygame.display.flip()和pygame.display.update()的區(qū)別及說明
這篇文章主要介紹了pygame.display.flip()和pygame.display.update()的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Python連接MySQL并使用fetchall()方法過濾特殊字符
這篇文章主要介紹了Python連接MySQL的方法并講解了如何使用fetchall()方法過濾特殊字符,示例環(huán)境為Ubuntu操作系統,需要的朋友可以參考下2016-03-03

