当前位置: 代码网 > it编程>前端脚本>Python > 使用python将CSV和Excel表格数据导入到Word表格

使用python将CSV和Excel表格数据导入到Word表格

2024年09月04日 Python 我要评论
引言在不同格式的文档之间进行数据传输是非常重要的操作。例如将csv和excel表格数据导入到word文档中,不仅可以实现数据的有效整合与展示,还能极大地提升工作效率和文档的专业性。无论是生成报告、制作

引言

在不同格式的文档之间进行数据传输是非常重要的操作。例如将csv和excel表格数据导入到word文档中,不仅可以实现数据的有效整合与展示,还能极大地提升工作效率和文档的专业性。无论是生成报告、制作统计分析还是编制业务文档,熟练掌握用python处理这些常见文档的数据,能帮助我们更灵活地管理和呈现信息,满足各种需求。本文将介绍如何使用python将csv和excel表格数据导入到word文档中并创建表格。

本文所使用的方法需要用到spire.doc for python,pypi:pip install spire.doc

用python导入csv数据到word表格

csv文件中的表格数据可以使用python标准库中的csv模块直接读取为字符串,然后我们再使用spire.doc for python中的方法和属性利用读取的数据在word文档中创建表格,即可实现csv表格数据到word文档的导入。以下是操作步骤示例:

  1. 导入所需模块。
  2. 创建document对象从而创建一个word文档。
  3. 使用document.addsection()方法再文档中创建一个节,再使用section.addtable()方法在节中创建一个表格。
  4. 创建表头单元格文本和数据行单元格文本的段落样式。
  5. 将表头数据写入表格并设置格式。
  6. 将其他数据写入表格并设置格式。
  7. 使用table.autofit(autofitbehaviortype)方法设置表格自动对齐方式。
  8. 使用document.savetofile()方法保存文档。
  9. 释放资源。

代码示例

from spire.doc import *
import csv

# 读取csv表格数据
with open('sample.csv', 'r', encoding='utf-8') as file:
    reader = csv.reader(file)
    tabledata = []
    for row in reader:
        tabledata.append(row)

# 创建document实例
doc = document()

# 添加一个章节和一个表格
section = doc.addsection()
table = section.addtable()

# 为表头和单元格创建段落样式
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)

# 向表格添加数据行
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)

# 自动调整表格大小
table.autofit(autofitbehaviortype.autofittowindow)

# 保存文档
doc.savetofile("output/csvtowordtable.docx", fileformat.docx2019)
doc.close()

结果文档

用python导入excel数据到word表格

将excel文件表格数据导入word文档也可以用相似的操作进行。注意需要使用spire.xls for python(pypi:pip install spire.xls)导入excel工作表数据,然后写入word文档表格。以下是操作步骤:

  1. 导入所需模块。
  2. 创建document对象从而创建一个word文档。
  3. 使用document.addsection()方法再文档中创建一个节,再使用section.addtable()方法在节中创建一个表格。
  4. 创建workbook对象并使用workbook.loadfromfile()方法载入excel文件。
  5. 使用workbook.worksheets.get_item()方法获取工作表。
  6. 创建表头单元格文本和数据行单元格文本的段落样式。
  7. 将表头数据写入表格并设置格式。
  8. 将其他数据写入表格并设置格式。
  9. 使用table.autofit(autofitbehaviortype)方法设置表格自动对齐方式。
  10. 使用document.savetofile()方法保存文档。
  11. 释放资源。

代码示例

from spire.doc import document, paragraphstyle, verticalalignment, borderstyle, color, fileformat
from spire.xls import workbook

# 创建document实例
doc = document()

# 添加一个章节和一个表格
section = doc.addsection()
table = section.addtable()

# 创建workbook实例并加载一个excel文件
workbook = workbook()
workbook.loadfromfile("sample.xlsx")

worksheet = workbook.worksheets.get_item(0)

# 为表头和单元格创建段落样式
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)

# 自动调整表格大小
table.autofit(autofitbehaviortype.autofittowindow)

# 保存文档
doc.savetofile("output/exceltabletoword.docx", fileformat.docx2019)
doc.close()

结果文档

本文介绍了如何使用python将csv和excel表格数据导入word文档并创建表格。

到此这篇关于使用python将csv和excel表格数据导入到word表格的文章就介绍到这了,更多相关python csv和excel数据导入word内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com