你的代码对我很有用。把它插入数据帧怎么样?import pandas as pd
from docx.api import Document
document = Document('test_word.docx')
table = document.tables[0]
data = []
keys = None
for i, row in enumerate(table.rows):
text = (cell.text for cell in row.cells)
if i == 0:
keys = tuple(text)
continue
row_data = dict(zip(keys, text))
data.append(row_data)
print (data)
df = pd.DataFrame(data)
如何显示该表中的特定行和列?
我们可以使用iloc根据索引提取行和列# iloc[row,columns]
df.iloc[0,:].tolist() # [5,6,7,8] - row index 0
df.iloc[:,0].tolist() # [5,9,13,17] - column index 0
df.iloc[0,0] # 5 - cell(0,0)
df.iloc[1:,2].tolist() # [11,15,19] - column index 2, but skip first row
等等。。。
但是,如果列有名称(在本例中是数字),则可以这样做:#df["name"].tolist()
df[1].tolist() # [5,6,7,8] - column with name 1print(df)
打印,这就是我的示例文档中表格的外观。1 2 3 4
0 5 6 7 8
1 9 10 11 12
2 13 14 15 16
3 17 18 19 20