在word中读取和写入中的表格

 1 from docx import Document
 2 
 3 word =Document(r'表格名称.docx')
 4 
 5 #读取表格
 6 
 7 tables=word.tables
 8 
 9 for i in tables[:]:
10 
11   for j,row in enumerate(i.rows[:]):
12 
13     row_content=[]
14 
15     for cell in row.cells[:]  :   #读取每行中的所有单元格
16 
17       c=cell.text
18 
19       row_content.append(c)
20 
21     print(row_content)    #打印一行的信息
22 
23  
24 
25 #写入表格
26 
27 data=[
28 
29 ['学号','姓名','成绩'],
30 
31 [101,'李四',95],
32 
33 [102,'张三',90],
34 
35 [103,'王五',86]
36 
37 ]
38 
39 table=word.add_table(rows=4,cols=3)
40 
41 for row  in range(4):
42 
43   cells=table.rows[row].cells
44 
45    for  col  in   range(3):
46 
47     cells[col].text=str(data[row][col])
原文地址:https://www.cnblogs.com/energetic/p/13052388.html