使用python將CSV和Excel表格數(shù)據(jù)導入到Word表格
引言
在不同格式的文檔之間進行數(shù)據(jù)傳輸是非常重要的操作。例如將CSV和Excel表格數(shù)據(jù)導入到Word文檔中,不僅可以實現(xiàn)數(shù)據(jù)的有效整合與展示,還能極大地提升工作效率和文檔的專業(yè)性。無論是生成報告、制作統(tǒng)計分析還是編制業(yè)務文檔,熟練掌握用Python處理這些常見文檔的數(shù)據(jù),能幫助我們更靈活地管理和呈現(xiàn)信息,滿足各種需求。本文將介紹如何使用Python將CSV和Excel表格數(shù)據(jù)導入到Word文檔中并創(chuàng)建表格。
本文所使用的方法需要用到Spire.Doc for Python,PyPI:pip install Spire.Doc
用Python導入CSV數(shù)據(jù)到Word表格
CSV文件中的表格數(shù)據(jù)可以使用Python標準庫中的csv模塊直接讀取為字符串,然后我們再使用Spire.Doc for Python中的方法和屬性利用讀取的數(shù)據(jù)在Word文檔中創(chuàng)建表格,即可實現(xiàn)CSV表格數(shù)據(jù)到Word文檔的導入。以下是操作步驟示例:
- 導入所需模塊。
- 創(chuàng)建
Document對象從而創(chuàng)建一個Word文檔。 - 使用
Document.AddSection()方法再文檔中創(chuàng)建一個節(jié),再使用Section.AddTable()方法在節(jié)中創(chuàng)建一個表格。 - 創(chuàng)建表頭單元格文本和數(shù)據(jù)行單元格文本的段落樣式。
- 將表頭數(shù)據(jù)寫入表格并設置格式。
- 將其他數(shù)據(jù)寫入表格并設置格式。
- 使用
Table.AutoFit(AutoFitBehaviorType)方法設置表格自動對齊方式。 - 使用
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導入Excel數(shù)據(jù)到Word表格
將Excel文件表格數(shù)據(jù)導入Word文檔也可以用相似的操作進行。注意需要使用Spire.XLS for Python(PyPI:pip install Spire.XLS)導入Excel工作表數(shù)據(jù),然后寫入Word文檔表格。以下是操作步驟:
- 導入所需模塊。
- 創(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ù)據(jù)寫入表格并設置格式。
- 使用
Table.AutoFit(AutoFitBehaviorType)方法設置表格自動對齊方式。 - 使用
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ù)導入Word文檔并創(chuàng)建表格。
到此這篇關(guān)于使用python將CSV和Excel表格數(shù)據(jù)導入到Word表格的文章就介紹到這了,更多相關(guān)python CSV和Excel數(shù)據(jù)導入Word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何實現(xiàn)動態(tài)數(shù)組
這篇文章主要介紹了Python如何實現(xiàn)動態(tài)數(shù)組,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
如何使用Python生成4位數(shù)的隨機數(shù)字
本文討論了如何使用randint() 和randrange() 方法來生成一個四位數(shù)的數(shù)字,此外,我們還討論了另一種擁有隨機四位數(shù)號碼的途徑,感興趣的朋友跟隨小編一起看看吧2023-10-10
教你用Type Hint提高Python程序開發(fā)效率
本文通過介紹和實例教大家如何利用Type Hint來提升Python程序開發(fā)效率,對大家使用python開發(fā)很有幫助,有需要的參考學習。2016-08-08
Python基于多線程實現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫的方法
這篇文章主要介紹了Python基于多線程實現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫的方法,結(jié)合實例形式分析了Python使用數(shù)據(jù)庫類與多線程類進行數(shù)據(jù)抓取與寫入數(shù)據(jù)庫操作的具體使用技巧,需要的朋友可以參考下2018-06-06
python3美化表格數(shù)據(jù)輸出結(jié)果的實現(xiàn)代碼
本文介紹了兩種表格數(shù)據(jù)的打印工具:tabulate和prettytable的安裝與基本使用方法,通過實例講解的非常詳細,需要的朋友參考下吧2021-04-04

