把word中表格转成excle文件

把word中表格转成excle文件
python 复制代码
from docx import Document
from openpyxl import Workbook
from pathlib import Path

# 打开 Word 文档
document = Document('./weather_report.docx')
tables = document.tables

# 输出文件路径
output_file = Path('./weather_report.xlsx')

# 如果文件已存在,删除旧文件
if output_file.exists():
    print(f"🚮 {output_file} 已存在,删除...")
    output_file.unlink()

# 创建 Excel 工作簿
merged_wb = Workbook()

# 如果 Word 中没有表格,跳过保存
if not tables:
    print("❌ Word 文档中未找到任何表格。")
else:
    for i, t in enumerate(tables, start=1):
        # 第一个表使用默认的 active sheet,其余的新增
        if i == 1:
            ws = merged_wb.active
            ws.title = f"Table{i}"
        else:
            ws = merged_wb.create_sheet(title=f"Table{i}")

        for row in t.rows:
            row_data = [cell.text.strip() for cell in row.cells]
            ws.append(row_data)

    # 保存 Excel 文件
    merged_wb.save(output_file)
    print(f"✅ 所有表格已成功保存到 {output_file}")
相关推荐
Boilermaker19921 小时前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
aramae1 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周1 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
lixzest2 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
沉默媛2 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
_Chipen3 小时前
C++基础问题
开发语言·c++
止观止3 小时前
JavaScript对象创建9大核心技术解析
开发语言·javascript·ecmascript
screenCui4 小时前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
linux kernel5 小时前
第七讲:C++中的string类
开发语言·c++
玩代码5 小时前
Java线程池原理概述
java·开发语言·线程池