随心贴一位五百年的大魔法师
from docx import Document
def extract_text_from_table(doc_path):
# 打开Word文档
doc = Document(doc_path)
text = ""
# 遍历文档中的所有表格
for table in doc.tables: # 遍历表格
for row in table.rows: # 遍历行
for cell in row.cells: # 遍历单元格
# 将单元格文本添加到字符串中
text += cell.text + "\n"
return text
# 提取Word文档中表格的文本
table_text = extract_text_from_table(r'your_path/yourdocx.docx')
# 将提取的文本写入到TXT文件
txt_path = r'your_path/yourtxt.txt'
with open(txt_path, 'w', encoding='utf-8') as file:
file.write(table_text)