欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word表格

 更新時間:2024年09月03日 09:59:12   作者:Eiceblue  
在不同格式的文檔之間進(jìn)行數(shù)據(jù)傳輸是非常重要的操作,例如將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word文檔中,不僅可以實現(xiàn)數(shù)據(jù)的有效整合與展示,還能極大地提升工作效率和文檔的專業(yè)性,本文將介紹如何使用Python將CSV和Excel表格數(shù)據(jù)導(dǎo)入到Word文檔中并創(chuàng)建表格

引言

在不同格式的文檔之間進(jìn)行數(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標(biāo)準(zhǔn)庫中的csv模塊直接讀取為字符串,然后我們再使用Spire.Doc for Python中的方法和屬性利用讀取的數(shù)據(jù)在Word文檔中創(chuàng)建表格,即可實現(xiàn)CSV表格數(shù)據(jù)到Word文檔的導(dǎo)入。以下是操作步驟示例:

  1. 導(dǎo)入所需模塊。
  2. 創(chuàng)建Document對象從而創(chuàng)建一個Word文檔。
  3. 使用Document.AddSection()方法再文檔中創(chuàng)建一個節(jié),再使用Section.AddTable()方法在節(jié)中創(chuàng)建一個表格。
  4. 創(chuàng)建表頭單元格文本和數(shù)據(jù)行單元格文本的段落樣式。
  5. 將表頭數(shù)據(jù)寫入表格并設(shè)置格式。
  6. 將其他數(shù)據(jù)寫入表格并設(shè)置格式。
  7. 使用Table.AutoFit(AutoFitBehaviorType)方法設(shè)置表格自動對齊方式。
  8. 使用Document.SaveToFile()方法保存文檔。
  9. 釋放資源。

代碼示例

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文檔也可以用相似的操作進(jìn)行。注意需要使用Spire.XLS for Python(PyPI:pip install Spire.XLS)導(dǎo)入Excel工作表數(shù)據(jù),然后寫入Word文檔表格。以下是操作步驟:

  1. 導(dǎo)入所需模塊。
  2. 創(chuàng)建Document對象從而創(chuàng)建一個Word文檔。
  3. 使用Document.AddSection()方法再文檔中創(chuàng)建一個節(jié),再使用Section.AddTable()方法在節(jié)中創(chuàng)建一個表格。
  4. 創(chuàng)建Workbook對象并使用Workbook.LoadFromFile()方法載入Excel文件。
  5. 使用Workbook.Worksheets.get_Item()方法獲取工作表。
  6. 創(chuàng)建表頭單元格文本和數(shù)據(jù)行單元格文本的段落樣式。
  7. 將表頭數(shù)據(jù)寫入表格并設(shè)置格式。
  8. 將其他數(shù)據(jù)寫入表格并設(shè)置格式。
  9. 使用Table.AutoFit(AutoFitBehaviorType)方法設(shè)置表格自動對齊方式。
  10. 使用Document.SaveToFile()方法保存文檔。
  11. 釋放資源。

代碼示例

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)文章

最新評論