把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}")
相关推荐
小羊没烦恼!5 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹5 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
伞伞悦读5 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车5 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁5 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
kybs19915 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
孙启超5 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int5 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
CoderYanger5 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法