Python将Word转换为Excel

现有大量的Word文档,每个文档中有大量的表格,需要将其转换为Excel。

Python处理源码

python 复制代码
# 需要安装pip install xlsxwriter
import pandas as pd
from docx import Document
from pathlib import Path
from datetime import datetime

def process_docx(filepath):
    # 处理Word文档的主函数
    doc = Document(filepath)
    # 示例处理逻辑:提取所有段落文本
    content = [p.text for p in doc.paragraphs if p.text.strip()]
    print(f"成功处理文档: {filepath}")
    data = []
    monitor_time = ""
    # 提取监测时间
    for paragraph in doc.paragraphs:
        # print(paragraph.text)
        if "第12次:" in paragraph.text:
            monitor_time = parse_monitor_time(paragraph.text)
            print(f"提取监测时间: {monitor_time}")
            break

    # 处理所有表格
    index = 0
    for table in doc.tables:
        # 检查是否为数据表格(包含房屋编号列)
        if len(table.columns) >= 7 and "成果表" in table.cell(0,0).text:
            # print(table.cell(0,0).text)
            for row in table.rows[2:]:  # 跳过标题行
                first_cell_text = row.cells[0].text.strip()  # 获取第一个单元格的文本并去除首尾空格
                if "备注" in first_cell_text:  # 如果第一个单元格包含"备注"
                    continue  # 跳过该行
                cells = [cell.text.replace("\n", "").replace("\r", "").strip() for cell in row.cells]
                if len(cells) >= 7:  # 确保数据完整
                    # 构建输出记录
                    record = {
                        '点号': f"{cells[0].replace(" ", "")}-{cells[1]}",
                        '初始值': cells[2],
                        '检测值': cells[3],
                        '累计值': cells[4],
                        '监测时间': monitor_time,
                        '上次监测时间': "2025/6/17 03:00"  # 根据备注补充
                    }
                    # print(record)
                    data.append(record)
    return data


def generate_excel(data, output_path):
    # 生成标准格式Excel
    df = pd.DataFrame(data) 

    # 补充固定字段
    df['备注'] = '无'

    # 字段顺序调整
    columns_order = [ '点号', '初始值', '检测值', '累计值', '监测时间', '上次监测时间', '备注' ]
    df = df.reindex(columns=columns_order)

    # 填充空值
    df['上次监测时间'] = '2025-06-01'

    # 保存Excel
    # df.to_excel(output_path, index=False)
    with pd.ExcelWriter(output_path, engine='xlsxwriter') as writer:
        df.to_excel(writer, index=False, sheet_name='Sheet1')  # 导出数据
        worksheet = writer.sheets['Sheet1']
        # 手动设置列宽(单位:字符宽度)
        worksheet.set_column('A:A', 38)  # 设置A列为15字符宽度
        worksheet.set_column('B:B', 12)  # 设置B列为10字符宽度
    print(f"Excel文件已生成: {output_path}")

print(f"Excel开始生成")
filepath=r"C:\Users\admin\Desktop\test.docx"
output_path=r"C:\Users\admin\Desktop\test.xlsx"
data = process_docx(filepath)
generate_excel(data, output_path)
print(f"Excel生成结束")

输入Word文档

word文档格式如下所示

输出Excel文档

相关推荐
飞翔的佩奇3 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance3 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博4 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
lxmyzzs5 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
萧鼎6 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
yujkss6 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python
yzx9910137 小时前
小程序开发APP
开发语言·人工智能·python·yolo
飞翔的佩奇7 小时前
【完整源码+数据集+部署教程】二维码与查找模式检测系统源码和数据集:改进yolo11-CSwinTransformer
python·yolo·计算机视觉·数据集·yolo11·二维码与查找模式检测
大霞上仙7 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel
Caven777 小时前
【pytorch】reshape的使用
pytorch·python