python读word中的表格和插入表格

打印 上一主题 下一主题

主题 878|帖子 878|积分 2634

读取word中的表格

偶尔间需要从word中读取表格数据。差别于excel,word中表格的对象属性是Table。
示例文档如下:


读取效果:
  1. 行: 6 , 列: 3
  2. ['物料', '数量', '单价']
  3. ['车轮', '2', '100']
  4. ['坐垫', '1', '20']
  5. ['车把', '1', '5']
  6. ['车锁', '1', '13']
  7. ['总加', '138']
复制代码
注意到表格末了一行有合并的单元格,目前的读取方法对于合并的单元格按照1个列单元读取!
代码:
  1. [/code] [code]import win32com.client as win32
  2. from win32com.client import constants
  3. import os
  4. doc_app = win32.gencache.EnsureDispatch('Word.Application')#打开word应用程序
  5. doc_app.Visible = True
  6. curr_path = os.getcwd()
  7. file_path = r'%s\带表格文档.docx'%curr_path
  8. doc = doc_app.Documents.Open(file_path)
  9. table = doc.Tables(1)
  10. print('行:',table.Rows.Count, ', 列:', table.Columns.Count)
  11. for row in table.Rows:#遍历表格每行
  12.         info = []
  13.         for cell in row.Cells:#遍历每行中的表格,即有效列
  14.                 info.append(cell.Range.Text[:-2])
  15.         print(info)
复制代码

读取word中带有合并单元格的表格



  1. print('有效单元格个数:',table.Range.Cells.Count)
  2. for cell in table.Range.Cells:
  3.         print(cell.RowIndex, ',', cell.ColumnIndex,',',cell.Range.Text[:-2])
复制代码
word的对象中,无法直接判定单元格是否列合并大概行合并的。可以间接通过table.Cells来访问有用的单元格。假如访问cell.Row大概cell.Column,碰到合并的单元格会报错。
运行结果:
  1. 有效单元格个数: 20
  2. 1 , 1 ,
  3. 1 , 2 , 物料
  4. 1 , 3 , 数量
  5. 1 , 4 , 单价
  6. 2 , 1 , 零件
  7. 2 , 2 , 车轮
  8. 2 , 3 , 2
  9. 2 , 4 , 100
  10. 3 , 2 , 坐垫
  11. 3 , 3 , 1
  12. 3 , 4 , 20
  13. 4 , 2 , 车把
  14. 4 , 3 , 1
  15. 4 , 4 , 5
  16. 5 , 2 , 车锁
  17. 5 , 3 , 1
  18. 5 , 4 , 13
  19. 6 , 1 ,
  20. 6 , 2 , 总加
  21. 6 , 3 , 138
复制代码
在word中插入表格

脚本效果:


脚本实现的内容:在新的word文档中插入一个表格,并写入脚本的内容。
  1. [/code] [code]import win32com.client as win32
  2. from win32com.client import constants
  3. import os
  4. doc_app = win32.gencache.EnsureDispatch('Word.Application')#打开word应用程序
  5. doc = doc_app.Documents.Add()
  6. doc_app.Visible = True
  7. last_parag = doc.Paragraphs.Last
  8. # 创建新的表格
  9. table = doc.Tables.Add(Range=last_parag.Range, NumRows=3, NumColumns=4)
  10. # 设置表格边框内外线
  11. table.Borders.InsideLineStyle = constants.wdLineStyleSingle
  12. table.Borders.OutsideLineStyle = constants.wdLineStyleDouble
  13. #写入表格
  14. cnt=0
  15. for row in table.Rows:#遍历表格每行
  16.         for cell in row.Cells:#遍历每行中的表格,即有效列
  17.                 cell.Range.Text = cnt
  18.                 cnt += 1
复制代码

插入第二个表格

  1. [/code] [code]#插入第二个表格
  2. last_parag = doc.Paragraphs.Last
  3. table2 = doc.Tables.Add(Range=last_parag.Range, NumRows=3, NumColumns=4)
  4. table2.Borders.InsideLineStyle = constants.wdLineStyleSingle
  5. table2.Borders.OutsideLineStyle = constants.wdLineStyleDouble
复制代码

插入效果:


第二个表格插入效果,看起来和第一个连在一起了
调解一下,在第二个表格背面插入一个新的空行,拉开一段的间隔。
  1. [/code] [code]#插入第二个表格
  2. doc.Paragraphs.Add()# 插入新的一行
  3. last_parag = doc.Paragraphs.Last #指向最后一行(刚插入的那一行)
  4. table2 = doc.Tables.Add(Range=last_parag.Range, NumRows=3, NumColumns=4)
  5. table2.Borders.InsideLineStyle = constants.wdLineStyleSingle
  6. table2.Borders.OutsideLineStyle = constants.wdLineStyleDouble
复制代码





遍历文档中的表格

  1. for table in doc.Tables:
  2.     print(table.Row(1).Cells(1).Range.Text) # 打印每个表格中左上角单元格的内容
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表