当前位置:首页>python>《告别复制粘贴!用Python一键将Excel批量嵌入Word,双击即可编辑,效率直接爆棚!》

《告别复制粘贴!用Python一键将Excel批量嵌入Word,双击即可编辑,效率直接爆棚!》

  • 2026-10-11 05:52:19
《告别复制粘贴!用Python一键将Excel批量嵌入Word,双击即可编辑,效率直接爆棚!》

“带你横跨办公自动化的数据江海”

@摸鱼

前言

关于摸鱼

(

ABOUT MOYU

)

闻道有先后,术业有专攻。

各位大佬朋友们好!

~我依旧是你们的老朋友摸鱼~

在职场摸爬滚打的这十多年里,我用Python悄悄干了不少“正事”——不知不觉攒下了一整套办公自动化的实用项目技巧。去年10月初创立了公众号 「码海听潮」 ,初衷很简单:把重复的劳动交给代码,把摸鱼的时间留给生活。

目前已经吭哧吭哧更新了100多篇原创文章,每一篇都是实操干货,不讲虚的,只聊怎么用代码真正解放双手,帮大家早点下班、准点摸鱼

好了,多了不说,少了不唠,今天给大佬们分享一款本人原创的【Excel批量嵌入Word】小工具,这小工具能把Excel表格以可编辑对象的形式直接嵌入Word文档,一个Excel文件里的多个工作表会自动分页嵌入,完整保留公式、样式和数据,双击就能编辑Excel,特别适合需要频繁把Excel插入Word但又要求保留编辑能力的场景。

《办公工具成品展示:》

《该办公工具的演示视频:》

《该办公工具核心功能亮点:》

01

 亮点1:嵌入式嵌入,保留完整编辑能力 🎯

        02

         亮点2:批量处理,一键搞定📱

            03

            亮点3:多工作表自动分页⚡

            《该办公工具代码展示:》

            下面,我就用python代码让各位大佬见识一下,什么叫"传统文化"遇上"赛博效率"

            import sysimport osfrom pathlib import Pathfrom typing import Optionalfrom datetime import datetimefrom PyQt6.QtWidgets import (    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,    QPushButton, QLabel, QLineEdit, QFileDialog, QTextEdit,     QGroupBox, QRadioButton, QMessageBox, QComboBox, QSpinBox, QDoubleSpinBox)from PyQt6.QtCore import Qtfrom PyQt6.QtGui import QFont, QTextCursorclass ModernButton(QPushButton):    """现代化按钮样式"""    def __init__(self, text: str, parent=None):        super().__init__(text, parent)        self.setCursor(Qt.CursorShape.PointingHandCursor)        self.setMinimumHeight(35)        self.setStyleSheet("""            QPushButton {                background-color: #4a8aff;                color: white;                border: none;                border-radius: 5px;                padding: 8px 16px;                font-weight: bold;            }            QPushButton:hover {                background-color: #5c9eff;            }            QPushButton:pressed {                background-color: #3a7ae5;            }            QPushButton:disabled {                background-color: #cccccc;            }        """)class MainWindow(QMainWindow):    """主窗口"""    def __init__(self):        super().__init__()        self.init_ui()    def init_ui(self):        """初始化UI"""        self.setWindowTitle("Excel批量嵌入Word工具")        self.setMinimumSize(900, 700)        # 中央部件        central_widget = QWidget()        self.setCentralWidget(central_widget)        # 主布局        main_layout = QVBoxLayout(central_widget)        main_layout.setSpacing(15)        main_layout.setContentsMargins(20, 20, 20, 20)        # 输入区域        input_group = QGroupBox("输入设置")        input_layout = QVBoxLayout(input_group)        # 输入路径选择        input_path_layout = QHBoxLayout()        self.input_path_edit = QLineEdit()        self.input_path_edit.setPlaceholderText("请选择Excel文件或文件夹...")        self.input_path_edit.setMinimumHeight(35)        input_browse_btn = ModernButton("浏览输入")        input_browse_btn.clicked.connect(self.browse_input)        input_path_layout.addWidget(self.input_path_edit)        input_path_layout.addWidget(input_browse_btn)        input_layout.addLayout(input_path_layout)        main_layout.addWidget(input_group)        # 输出区域        output_group = QGroupBox("输出设置")        output_layout = QVBoxLayout(output_group)        # 输出模式选择        output_mode_layout = QHBoxLayout()        output_mode_layout.addWidget(QLabel("输出位置:"))        self.same_folder_radio = QRadioButton("Excel所在文件夹")        self.same_folder_radio.setChecked(True)        self.custom_folder_radio = QRadioButton("自定义文件夹")        self.same_folder_radio.toggled.connect(lambda checked: self.toggle_output_path(not checked))        self.custom_folder_radio.toggled.connect(lambda checked: self.toggle_output_path(checked))        output_mode_layout.addWidget(self.same_folder_radio)        output_mode_layout.addWidget(self.custom_folder_radio)        output_mode_layout.addStretch()        output_layout.addLayout(output_mode_layout)        # 自定义输出路径        output_path_layout = QHBoxLayout()        self.output_path_edit = QLineEdit()        self.output_path_edit.setPlaceholderText("选择输出文件夹...")        self.output_path_edit.setEnabled(False)        self.output_path_edit.setMinimumHeight(35)        output_browse_btn = ModernButton("浏览输出")        output_browse_btn.clicked.connect(self.browse_output)        output_browse_btn.setEnabled(False)        output_path_layout.addWidget(self.output_path_edit)        output_path_layout.addWidget(output_browse_btn)        output_layout.addLayout(output_path_layout)        main_layout.addWidget(output_group)        # 页面设置区域        page_group = QGroupBox("页面设置 (厘米)")        page_layout = QHBoxLayout(page_group)        page_layout.setSpacing(15)        # 上边距        page_layout.addWidget(QLabel("上边距:"))        self.top_margin_spin = QSpinBox()        self.top_margin_spin.setRange(0, 10)        self.top_margin_spin.setValue(2)        self.top_margin_spin.setSuffix(" cm")        self.top_margin_spin.setMinimumWidth(80)        page_layout.addWidget(self.top_margin_spin)        # 下边距        page_layout.addWidget(QLabel("下边距:"))        self.bottom_margin_spin = QSpinBox()        self.bottom_margin_spin.setRange(0, 10)        self.bottom_margin_spin.setValue(2)        self.bottom_margin_spin.setSuffix(" cm")        self.bottom_margin_spin.setMinimumWidth(80)        page_layout.addWidget(self.bottom_margin_spin)        # 左边距        page_layout.addWidget(QLabel("左边距:"))        self.left_margin_spin = QDoubleSpinBox()        self.left_margin_spin.setRange(0, 10)        self.left_margin_spin.setValue(2.5)        self.left_margin_spin.setSuffix(" cm")        self.left_margin_spin.setSingleStep(0.5)        self.left_margin_spin.setMinimumWidth(80)        page_layout.addWidget(self.left_margin_spin)        # 右边距        page_layout.addWidget(QLabel("右边距:"))        self.right_margin_spin = QDoubleSpinBox()        self.right_margin_spin.setRange(0, 10)        self.right_margin_spin.setValue(1.5)        self.right_margin_spin.setSuffix(" cm")        self.right_margin_spin.setSingleStep(0.5)        self.right_margin_spin.setMinimumWidth(80)        page_layout.addWidget(self.right_margin_spin)        # 嵌入对象宽度        page_layout.addWidget(QLabel("宽度:"))        self.page_width_spin = QDoubleSpinBox()        self.page_width_spin.setRange(5, 30)        self.page_width_spin.setValue(16.5)        self.page_width_spin.setSuffix(" cm")        self.page_width_spin.setSingleStep(0.5)        self.page_width_spin.setMinimumWidth(80)        page_layout.addWidget(self.page_width_spin)        page_layout.addStretch()        main_layout.addWidget(page_group)        # 控制按钮        control_layout = QHBoxLayout()        self.convert_btn = ModernButton("开始转换")        self.convert_btn.setMinimumHeight(45)        self.convert_btn.setStyleSheet("""            QPushButton {                background-color: #4caf50;                font-size: 14px;            }            QPushButton:hover {                background-color: #66bb6a;            }        """)        self.cancel_btn = ModernButton("取消")        self.cancel_btn.setEnabled(False)        self.cancel_btn.setStyleSheet("""            QPushButton {                background-color: #f44336;            }            QPushButton:hover {                background-color: #ef5350;            }        """)        control_layout.addStretch()        control_layout.addWidget(self.convert_btn)        control_layout.addWidget(self.cancel_btn)        control_layout.addStretch()        main_layout.addLayout(control_layout)        # 日志区域        log_group = QGroupBox("处理日志")        log_layout = QVBoxLayout(log_group)        self.log_text = QTextEdit()        self.log_text.setReadOnly(True)        self.log_text.setMaximumHeight(300)        self.log_text.setFont(QFont("Consolas", 10))        log_control_layout = QHBoxLayout()        clear_log_btn = ModernButton("清空日志")        clear_log_btn.clicked.connect(self.clear_log)        log_control_layout.addStretch()        log_control_layout.addWidget(clear_log_btn)        log_layout.addWidget(self.log_text)        log_layout.addLayout(log_control_layout)        main_layout.addWidget(log_group)        # 应用样式        self.apply_styles()    def apply_styles(self):        """应用样式表"""        self.setStyleSheet("""            QMainWindow {                background-color: #f5f5f5;            }            QGroupBox {                font-weight: bold;                border: 2px solid #ddd;                border-radius: 8px;                margin-top: 10px;                padding-top: 10px;            }            QGroupBox::title {                subcontrol-origin: margin;                left: 10px;                padding: 0 5px 0 5px;            }            QLineEdit {                border: 2px solid #ddd;                border-radius: 5px;                padding: 5px;                background-color: white;            }            QLineEdit:focus {                border-color: #4a8aff;            }            QTextEdit {                border: 2px solid #ddd;                border-radius: 5px;                background-color: white;                font-family: Consolas;            }            QCheckBox, QRadioButton {                spacing: 8px;            }            QComboBox {                border: 2px solid #ddd;                border-radius: 5px;                padding: 5px;                min-height: 25px;            }            QSpinBox, QDoubleSpinBox {                border: 2px solid #ddd;                border-radius: 5px;                padding: 5px;                min-height: 25px;            }        """)    def browse_input(self):        """浏览输入路径"""        file_paths, _ = QFileDialog.getOpenFileNames(            self,             "选择Excel文件",             "",             "Excel文件 (*.xlsx *.xls *.xlsm)"        )        if file_paths:            if len(file_paths) == 1:                self.input_path_edit.setText(file_paths[0])            else:                folder_path = os.path.dirname(file_paths[0])                self.input_path_edit.setText(folder_path)                self.add_log(f"选择了 {len(file_paths)} 个文件", "info")        else:            folder = QFileDialog.getExistingDirectory(self, "选择文件夹")            if folder:                self.input_path_edit.setText(folder)    def browse_output(self):        """浏览输出路径"""        folder = QFileDialog.getExistingDirectory(self, "选择输出文件夹")        if folder:            self.output_path_edit.setText(folder)    def toggle_output_path(self, enabled: bool):        """切换输出路径编辑框状态"""        self.output_path_edit.setEnabled(enabled)        for child in self.findChildren(QPushButton):            if child.text() == "浏览输出":                child.setEnabled(enabled)                break    def add_log(self, message: str, level: str = "info"):        """添加日志"""        timestamp = datetime.now().strftime("%H:%M:%S")        color = "#000000"        if level == "error":            color = "#f44336"        elif level == "success":            color = "#4caf50"        elif level == "info":            color = "#2196f3"        formatted_msg = f'<span style="color: #888;">[{timestamp}]</span> <span style="color: {color};">{message}</span>'        self.log_text.append(formatted_msg)        cursor = self.log_text.textCursor()        cursor.movePosition(QTextCursor.MoveOperation.End)        self.log_text.setTextCursor(cursor)    def clear_log(self):        """清空日志"""        self.log_text.clear()def main():    """主函数"""    app = QApplication(sys.argv)    app.setStyle('Fusion')    window = MainWindow()    window.show()    sys.exit(app.exec())if __name__ == "__main__":    main()

            通过上面Python自动化脚本,仅用几秒钟的时间就完成原需手动操作数小时甚至数天的工作任务。从最初准备手动人工机械操作的麻木到用python实现高效自动化的畅快,工作效率获得指数级提升,终于实现了不加班熬夜的自由!

            大佬们也可以举一反三,参照上面的代码思路根据自己工作中的实际情况来具体问题具体分析,实现自己定制化的需求。

            《该办公工具的应用场景:》

            场景
            说明

            月度报表归档

            每月生成的利润表、资产负债表、现金流量表批量嵌入Word,方便存档和查阅

            审计资料整理

            审计底稿中的大量Excel数据表嵌入Word文档,保持可编辑性便于调整

            预算汇总报告

            各部门预算表批量嵌入,形成完整的预算报告文档

            发票清单归档

            进销项发票明细表嵌入Word,配合说明文字形成完整记录

            结语

            当Python遇见办公,牛马打工人终于笑出了猪叫声

            【职场人必看】每天早上一睁眼,想到又要面对:

            1.📊 堆积如山的Excel表格

            2.📑 机械重复的复制粘贴

            3.✍️ 永远改不完的各类文档

            4.诸如此类的更多........

            是不是连Ctrl+Alt+Delete的心都有了?

            别慌!别急,摸鱼这位“职场外挂”已经带着Python代码来拯救你了!

            感谢各位大佬观看,还望各位大佬抬抬贵手一键三连,多多关注点赞转发评论,大佬们的支持才是摸鱼孜孜不倦更新原创干货的动力!

            最新文章

            随机文章