python对word所有内容包括表格写到excel
时间: 2023-07-07 07:22:21 浏览: 42
你可以使用Python中的`python-docx`和`openpyxl`库来将Word文档中的内容写入Excel表格中,包括表格。以下是一个简单的示例代码:
```python
import openpyxl
from docx import Document
# 读取Word文档
document = Document('example.docx')
# 创建一个新的Excel工作簿
workbook = openpyxl.Workbook()
sheet = workbook.active
# 将Word文档中的内容逐行写入Excel表格中
for i, para in enumerate(document.paragraphs):
sheet.cell(row=i+1, column=1, value=para.text)
# 处理Word文档中的表格
for table in document.tables:
for i, row in enumerate(table.rows):
for j, cell in enumerate(row.cells):
sheet.cell(row=i+1, column=j+1, value=cell.text)
# 保存Excel文件
workbook.save('example.xlsx')
```
这个示例代码将会把名为`example.docx`的Word文档中的所有内容逐行写入到名为`example.xlsx`的Excel表格中的第一列,包括表格。你可以根据自己的实际情况进行修改。