使用python將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word表格
引言
在不同格式的文檔之間進(jìn)行數(shù)據(jù)傳輸是非常重要的操作。例如將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word文檔中,不僅可以實(shí)現(xiàn)數(shù)據(jù)的有效整合與展示,還能極大地提升工作效率和文檔的專(zhuān)業(yè)性。無(wú)論是生成報(bào)告、制作統(tǒng)計(jì)分析還是編制業(yè)務(wù)文檔,熟練掌握用Python處理這些常見(jiàn)文檔的數(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標(biāo)準(zhǔn)庫(kù)中的csv
模塊直接讀取為字符串,然后我們?cè)偈褂肧pire.Doc for Python中的方法和屬性利用讀取的數(shù)據(jù)在Word文檔中創(chuàng)建表格,即可實(shí)現(xiàn)CSV表格數(shù)據(jù)到Word文檔的導(dǎo)入。以下是操作步驟示例:
- 導(dǎo)入所需模塊。
- 創(chuàng)建
Document
對(duì)象從而創(chuàng)建一個(gè)Word文檔。 - 使用
Document.AddSection()
方法再文檔中創(chuàng)建一個(gè)節(jié),再使用Section.AddTable()
方法在節(jié)中創(chuàng)建一個(gè)表格。 - 創(chuàng)建表頭單元格文本和數(shù)據(jù)行單元格文本的段落樣式。
- 將表頭數(shù)據(jù)寫(xiě)入表格并設(shè)置格式。
- 將其他數(shù)據(jù)寫(xiě)入表格并設(shè)置格式。
- 使用
Table.AutoFit(AutoFitBehaviorType)
方法設(shè)置表格自動(dòng)對(duì)齊方式。 - 使用
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實(shí)例 doc = Document() # 添加一個(gè)章節(jié)和一個(gè)表格 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) # 自動(dòng)調(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文檔也可以用相似的操作進(jìn)行。注意需要使用Spire.XLS for Python(PyPI:pip install Spire.XLS
)導(dǎo)入Excel工作表數(shù)據(jù),然后寫(xiě)入Word文檔表格。以下是操作步驟:
- 導(dǎo)入所需模塊。
- 創(chuàng)建
Document
對(duì)象從而創(chuàng)建一個(gè)Word文檔。 - 使用
Document.AddSection()
方法再文檔中創(chuàng)建一個(gè)節(jié),再使用Section.AddTable()
方法在節(jié)中創(chuàng)建一個(gè)表格。 - 創(chuàng)建
Workbook
對(duì)象并使用Workbook.LoadFromFile()
方法載入Excel文件。 - 使用
Workbook.Worksheets.get_Item()
方法獲取工作表。 - 創(chuàng)建表頭單元格文本和數(shù)據(jù)行單元格文本的段落樣式。
- 將表頭數(shù)據(jù)寫(xiě)入表格并設(shè)置格式。
- 將其他數(shù)據(jù)寫(xiě)入表格并設(shè)置格式。
- 使用
Table.AutoFit(AutoFitBehaviorType)
方法設(shè)置表格自動(dòng)對(duì)齊方式。 - 使用
Document.SaveToFile()
方法保存文檔。 - 釋放資源。
代碼示例
from spire.doc import Document, ParagraphStyle, VerticalAlignment, BorderStyle, Color, FileFormat from spire.xls import Workbook # 創(chuàng)建Document實(shí)例 doc = Document() # 添加一個(gè)章節(jié)和一個(gè)表格 section = doc.AddSection() table = section.AddTable() # 創(chuàng)建Workbook實(shí)例并加載一個(gè)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) # 自動(dòng)調(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)組
這篇文章主要介紹了Python如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)組,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11使用python制作一個(gè)為hex文件增加版本號(hào)的腳本實(shí)例
今天小編就為大家分享一篇使用python制作一個(gè)為hex文件增加版本號(hào)的腳本實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06如何使用Python生成4位數(shù)的隨機(jī)數(shù)字
本文討論了如何使用randint() 和randrange() 方法來(lái)生成一個(gè)四位數(shù)的數(shù)字,此外,我們還討論了另一種擁有隨機(jī)四位數(shù)號(hào)碼的途徑,感興趣的朋友跟隨小編一起看看吧2023-10-10教你用Type Hint提高Python程序開(kāi)發(fā)效率
本文通過(guò)介紹和實(shí)例教大家如何利用Type Hint來(lái)提升Python程序開(kāi)發(fā)效率,對(duì)大家使用python開(kāi)發(fā)很有幫助,有需要的參考學(xué)習(xí)。2016-08-08三個(gè)520專(zhuān)屬Python表白代碼分享
快到520了,這篇文章主要為大家介紹了三個(gè)520專(zhuān)屬Python表白代碼。文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起動(dòng)手試一試2022-05-05使用opencv中匹配點(diǎn)對(duì)的坐標(biāo)提取方式
這篇文章主要介紹了使用opencv中匹配點(diǎn)對(duì)的坐標(biāo)提取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python基于多線程實(shí)現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Python基于多線程實(shí)現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了Python使用數(shù)據(jù)庫(kù)類(lèi)與多線程類(lèi)進(jìn)行數(shù)據(jù)抓取與寫(xiě)入數(shù)據(jù)庫(kù)操作的具體使用技巧,需要的朋友可以參考下2018-06-06python3美化表格數(shù)據(jù)輸出結(jié)果的實(shí)現(xiàn)代碼
本文介紹了兩種表格數(shù)據(jù)的打印工具:tabulate和prettytable的安裝與基本使用方法,通過(guò)實(shí)例講解的非常詳細(xì),需要的朋友參考下吧2021-04-04