翼度科技»论坛 编程开发 python 查看内容

Python 在Word中创建表格并填入数据、图片

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
在Word中,表格是一个强大的工具,它可以帮助你更好地组织、呈现和分析信息。本文将介绍如何使用Python在Word中创建表格并填入数据、图片,以及设置表格样式等。
Python Word库:
要使用Python在Word中创建或操作表格,需要先将Spire.Doc for Python这个第三方库安装到项目中.
  1. pip install Spire.Doc
复制代码
 
示例代码1:使用Python在Word中创建表格并填充数据
  1. import math
  2. from spire.doc import *
  3. from spire.doc.common import *
  4. # 创建Document对象
  5. doc = Document()
  6. # 添加一节
  7. section = doc.AddSection()
  8. # 创建一个表格
  9. table = section.AddTable()
  10. # 指定表格数据
  11. header_data = ["商品名称", "单位", "数量", "单价"]
  12. row_data = [ ["底板-1","件","20946","2.9"],
  13.                 ["定位板-2","张","38931","1.5"],
  14.                 ["整平模具-3","组","32478","1.1"],
  15.                 ["后壳FD1042-4","组","21162","0.6"],
  16.                 ["棍子-5","组","66517","1.2"]]
  17.                
  18. # 设置表格的行数和列数
  19. table.ResetCells(len(row_data) + 1, len(header_data))
  20. # 设置表格自适应窗口
  21. table.AutoFit(AutoFitBehaviorType.AutoFitToWindow)
  22. # 设置标题行
  23. headerRow = table.Rows[0]
  24. headerRow.IsHeader = True
  25. headerRow.Height = 23
  26. headerRow.RowFormat.BackColor = Color.get_Orange()
  27. # 在标题行填充数据并设置文本格式
  28. i = 0
  29. while i < len(header_data):
  30.     headerRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle
  31.     paragraph = headerRow.Cells[i].AddParagraph()
  32.     paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
  33.     txtRange = paragraph.AppendText(header_data[i])
  34.     txtRange.CharacterFormat.Bold = True
  35.     txtRange.CharacterFormat.FontSize = 12
  36.     i += 1
  37. # 将数据填入其余各行并设置文本格式
  38. r = 0
  39. while r < len(row_data):
  40.     dataRow = table.Rows[r + 1]
  41.     dataRow.Height = 20
  42.     dataRow.HeightType = TableRowHeightType.Exactly
  43.     c = 0
  44.     while c < len(row_data[r]):
  45.         dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle
  46.         paragraph = dataRow.Cells[c].AddParagraph()
  47.         paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
  48.         txtRange =  paragraph.AppendText(row_data[r][c])
  49.         txtRange.CharacterFormat.FontSize = 11
  50.         c += 1
  51.     r += 1
  52. # 设置交替行颜色
  53. for j in range(1, table.Rows.Count):
  54.     if math.fmod(j, 2) == 0:
  55.         row2 = table.Rows[j]
  56.         for f in range(row2.Cells.Count):
  57.             row2.Cells[f].CellFormat.BackColor = Color.get_LightGray()
  58. # 保存文件
  59. doc.SaveToFile("Word表格.docx", FileFormat.Docx2016)
复制代码
以下示例通过Section.AddTable() 方法在Word文档中添加了一个表格,然后将列表中的数据填充到了指定的单元格。此外Spire.Doc for Python库还提供了接口设置单元格样式等。
输出结果:

 
代码示例2:使用Python在Word表格中插入图片
  1. from spire.doc import *
  2. from spire.doc.common import *
  3. inputFile = "表格示例.docx"
  4. outputFile = "插入图片到表格.docx"
  5. # 创建Document对象
  6. doc = Document()
  7. # 加载Word文档
  8. doc.LoadFromFile(inputFile)
  9. # 获取文档中第一个表格
  10. table = doc.Sections[0].Tables[0]
  11. # 将图片添加到指定单元格并设置图片大小
  12. cell = table.Rows[1].Cells[1]
  13. picture = cell.Paragraphs[0].AppendPicture("python.png")
  14. picture.Width = 80
  15. picture.Height = 80
  16. cell = table.Rows[2].Cells[1]
  17. picture = cell.Paragraphs[0].AppendPicture("java.jpg")
  18. picture.Width = 80
  19. picture.Height = 80
  20. # 保存结果文件
  21. doc.SaveToFile(outputFile, FileFormat.Docx)
  22. doc.Close()
复制代码
从以上代码可以看出,要在Word表格中插入图片,需要先获取指定的单元格,然后使用TableCell.Paragraphs[index].AppendPicture() 方法插入图片。
输出结果:

 
Spire.Doc for Python库还支持对Word中的表格进行其他操作,如添加、删除复制行或列、合并或拆分单元格等。更多示例demo可查看:
https://www.e-iceblue.cn/docforpython/spire-doc-for-python-program-guide-content.html


对于水印问题,可以点击申请临时授权移除,或者发送邮件到sales@e-iceblue.com。

来源:https://www.cnblogs.com/Yesi/p/18065520
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具