使用python將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word表格
引言
在不同格式的文檔之間進行數(shù)據(jù)傳輸是非常重要的操作。例如將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word文檔中,不僅可以實現(xiàn)數(shù)據(jù)的有效整合與展示,還能極大地提升工作效率和文檔的專業(yè)性。無論是生成報告、制作統(tǒng)計分析還是編制業(yè)務(wù)文檔,熟練掌握用Python處理這些常見文檔的數(shù)據(jù),能幫助我們更靈活地管理和呈現(xiàn)信息,滿足各種需求。本文將介紹如何使用Python將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word文檔中并創(chuàng)建表格。
本文所使用的方法需要用到Spire.Doc for Python,PyPI:pip install Spire.Doc
用Python導(dǎo)入CSV數(shù)據(jù)到Word表格
CSV文件中的表格數(shù)據(jù)可以使用Python標準庫中的csv模塊直接讀取為字符串,然后我們再使用Spire.Doc for Python中的方法和屬性利用讀取的數(shù)據(jù)在Word文檔中創(chuàng)建表格,即可實現(xiàn)CSV表格數(shù)據(jù)到Word文檔的導(dǎo)入。以下是操作步驟示例:
- 導(dǎo)入所需模塊。
- 創(chuàng)建
Document對象從而創(chuàng)建一個Word文檔。 - 使用
Document.AddSection()方法再文檔中創(chuàng)建一個節(jié),再使用Section.AddTable()方法在節(jié)中創(chuàng)建一個表格。 - 創(chuàng)建表頭單元格文本和數(shù)據(jù)行單元格文本的段落樣式。
- 將表頭數(shù)據(jù)寫入表格并設(shè)置格式。
- 將其他數(shù)據(jù)寫入表格并設(shè)置格式。
- 使用
Table.AutoFit(AutoFitBehaviorType)方法設(shè)置表格自動對齊方式。 - 使用
Document.SaveToFile()方法保存文檔。 - 釋放資源。
代碼示例
from spire.doc import *
import csv
# 讀取CSV表格數(shù)據(jù)
with open('Sample.csv', 'r', encoding='utf-8') as file:
reader = csv.reader(file)
tableData = []
for row in reader:
tableData.append(row)
# 創(chuàng)建Document實例
doc = Document()
# 添加一個章節(jié)和一個表格
section = doc.AddSection()
table = section.AddTable()
# 為表頭和單元格創(chuàng)建段落樣式
headerStyle = ParagraphStyle(doc)
headerStyle.Name = "TableHeader"
headerStyle.CharacterFormat.FontName = "Arial"
headerStyle.CharacterFormat.FontSize = 12
headerStyle.CharacterFormat.Bold = True
doc.Styles.Add(headerStyle)
cellStyle = ParagraphStyle(doc)
cellStyle.Name = "TableCell"
cellStyle.CharacterFormat.FontName = "Arial"
cellStyle.CharacterFormat.FontSize = 11
doc.Styles.Add(cellStyle)
# 向表格添加表頭行
headerRow = tableData[0]
tableRow = table.AddRow()
for cell in headerRow:
tableCell = tableRow.AddCell()
paragraph = tableCell.AddParagraph()
paragraph.AppendText(cell)
paragraph.ApplyStyle(headerStyle.Name)
tableCell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
tableCell.CellFormat.Borders.BorderType(BorderStyle.Single)
tableCell.CellFormat.Borders.Color = Color.get_Black()
tableCell.CellFormat.Borders.LineWidth(1.8)
# 向表格添加數(shù)據(jù)行
for row in tableData[1:]:
tableRow = table.AddRow()
for cell in row:
tableCell = tableRow.Cells[row.index(cell)]
paragraph = tableCell.AddParagraph()
paragraph.AppendText(cell)
paragraph.ApplyStyle(cellStyle.Name)
tableCell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
tableCell.CellFormat.Borders.BorderType(BorderStyle.Single)
tableCell.CellFormat.Borders.Color = Color.get_Black()
tableCell.CellFormat.Borders.LineWidth(0.8)
# 自動調(diào)整表格大小
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow)
# 保存文檔
doc.SaveToFile("output/CSVToWordTable.docx", FileFormat.Docx2019)
doc.Close()
結(jié)果文檔

用Python導(dǎo)入Excel數(shù)據(jù)到Word表格
將Excel文件表格數(shù)據(jù)導(dǎo)入Word文檔也可以用相似的操作進行。注意需要使用Spire.XLS for Python(PyPI:pip install Spire.XLS)導(dǎo)入Excel工作表數(shù)據(jù),然后寫入Word文檔表格。以下是操作步驟:
- 導(dǎo)入所需模塊。
- 創(chuàng)建
Document對象從而創(chuàng)建一個Word文檔。 - 使用
Document.AddSection()方法再文檔中創(chuàng)建一個節(jié),再使用Section.AddTable()方法在節(jié)中創(chuàng)建一個表格。 - 創(chuàng)建
Workbook對象并使用Workbook.LoadFromFile()方法載入Excel文件。 - 使用
Workbook.Worksheets.get_Item()方法獲取工作表。 - 創(chuàng)建表頭單元格文本和數(shù)據(jù)行單元格文本的段落樣式。
- 將表頭數(shù)據(jù)寫入表格并設(shè)置格式。
- 將其他數(shù)據(jù)寫入表格并設(shè)置格式。
- 使用
Table.AutoFit(AutoFitBehaviorType)方法設(shè)置表格自動對齊方式。 - 使用
Document.SaveToFile()方法保存文檔。 - 釋放資源。
代碼示例
from spire.doc import Document, ParagraphStyle, VerticalAlignment, BorderStyle, Color, FileFormat
from spire.xls import Workbook
# 創(chuàng)建Document實例
doc = Document()
# 添加一個章節(jié)和一個表格
section = doc.AddSection()
table = section.AddTable()
# 創(chuàng)建Workbook實例并加載一個Excel文件
workbook = Workbook()
workbook.LoadFromFile("Sample.xlsx")
worksheet = workbook.Worksheets.get_Item(0)
# 為表頭和單元格創(chuàng)建段落樣式
headerStyle = ParagraphStyle(doc)
headerStyle.Name = "TableHeader"
headerStyle.CharacterFormat.FontName = "Arial"
headerStyle.CharacterFormat.FontSize = 12
headerStyle.CharacterFormat.Bold = True
doc.Styles.Add(headerStyle)
cellStyle = ParagraphStyle(doc)
cellStyle.Name = "TableCell"
cellStyle.CharacterFormat.FontName = "Arial"
cellStyle.CharacterFormat.FontSize = 11
doc.Styles.Add(cellStyle)
print(worksheet.AllocatedRange.ColumnCount)
print(worksheet.AllocatedRange.ColumnCount)
headerRow = table.AddRow()
for i in range(worksheet.AllocatedRange.ColumnCount):
cell = headerRow.AddCell()
paragraph = cell.AddParagraph()
paragraph.AppendText(worksheet.AllocatedRange.get_Item(1, i + 1).Text)
paragraph.ApplyStyle(headerStyle.Name)
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
cell.CellFormat.Borders.BorderType(BorderStyle.Single)
cell.CellFormat.Borders.Color = Color.get_Black()
cell.CellFormat.Borders.LineWidth(1.8)
for j in range(1, worksheet.AllocatedRange.RowCount):
dataRow = table.AddRow()
for k in range(worksheet.AllocatedRange.ColumnCount):
cell = dataRow.Cells.get_Item(k)
paragraph = cell.AddParagraph()
paragraph.AppendText(worksheet.AllocatedRange.get_Item(j + 1, k + 1).Value)
paragraph.ApplyStyle(cellStyle.Name)
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
cell.CellFormat.Borders.BorderType(BorderStyle.Single)
cell.CellFormat.Borders.Color = Color.get_Black()
cell.CellFormat.Borders.LineWidth(0.8)
# 自動調(diào)整表格大小
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow)
# 保存文檔
doc.SaveToFile("output/ExcelTableToWord.docx", FileFormat.Docx2019)
doc.Close()
結(jié)果文檔

本文介紹了如何使用Python將CSV和Excel表格數(shù)據(jù)導(dǎo)入Word文檔并創(chuàng)建表格。
到此這篇關(guān)于使用python將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word表格的文章就介紹到這了,更多相關(guān)python CSV和Excel數(shù)據(jù)導(dǎo)入Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中staticmethod和classmethod的作用與區(qū)別
今天小編就為大家分享一篇關(guān)于Python中staticmethod和classmethod的作用與區(qū)別,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Python中axis=0與axis=1指的方向有什么不同詳解
對數(shù)據(jù)進行操作時,經(jīng)常需要在橫軸方向或者數(shù)軸方向?qū)?shù)據(jù)進行操作,這時需要設(shè)定參數(shù)axis的值,下面這篇文章主要給大家介紹了關(guān)于Python中axis=0與axis=1指的方向有什么不同的相關(guān)資料,需要的朋友可以參考下2024-01-01
Python在字典中獲取帶權(quán)重的隨機值實現(xiàn)方式
這篇文章主要介紹了Python在字典中獲取帶權(quán)重的隨機值,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11
Python OpenCV實現(xiàn)基于模板的圖像拼接
基于特征點的圖像拼接如果是多張圖,每次計算變換矩陣,都有誤差,最后可以圖像拼完就變形很大,基于模板的方法可以很好的解決這一問題,本文就來和大家具體聊聊2022-10-10
python接口自動化之ConfigParser配置文件的使用詳解
這篇文章主要介紹了python接口自動化之ConfigParser配置文件的使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
淺談python requests 的put, post 請求參數(shù)的問題
今天小編就為大家分享一篇淺談python requests 的put, post 請求參數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python+PyQt構(gòu)建自動化定時任務(wù)執(zhí)行工具詳細代碼示例
在日常工作中,我們常常會用到需要周期性執(zhí)行的任務(wù),這篇文章主要介紹了Python+PyQt構(gòu)建自動化定時任務(wù)執(zhí)行工具的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-09-09

