【Excel批处理】一键批量将Excel转Word和PDF,保持图片,文字等原样式

32 阅读3分钟

功能视频

www.bilibili.com/video/BV1QA…

前言

大家好,我是老罗软件,最近公司有一个变态的需求,有一堆Excel表格,需要转换成word和pdf。单个excel是很容易操作的,但多个文件就相当耗费人力,今天老罗就是帮大家来解决这个问题的。

注意: 转换时保持了原有表格,图片等样式。数据安全,本地操作,支持断网使用

需求描述

有一个复杂表格excel:

还有一个图文表格:

都需要保持原样式然后转成pdf和word, 转换结果如下:

解决方案

关注公众号:”老罗软件“ , 可以获取到解决方案软件下载。

这是一个专业解决excel多文件处理的方案, 找到 Excel批处理, 然后在弹出的框里点击 ”格式转换“。

软件打开后, 我们设置好界面的参数:

下面我来详细介绍下参数意思。

文件目录: 要处理转化的excel文件。
转换格式:  设置你要转换成pdf还是word。

设置好之后,点击开始处理, 程序会一条一条处理, 执行完,打开结果文件,就是转换好的文件。

如果您有疑问可以一起来探讨,功能就介绍到 这里 ,希望能帮助大家,感谢!!!

技术实现

非技术人员不需要观看!! 这里设计到的技术复杂, 我也就就简单讲解实现原理。

软件是基于Python开发的现代化办公自动化软件,主要使用了如下技术架构:

  1. PySide6 (Qt6) - 现代化GUI界面框架:
  2. springboot: excel的数据脱敏是通过后端java实现的。
  3. 文件处理:os.walk() - 递归遍历目录结构。
  4. http请求: requests框架。

部分代码解析

项目的 开始 按钮,会开启一个QThread线程去处理,首先是获取excel目录, 然后通过os.walk遍历目录获取到所有文件,然后一个一个进行处理,处理的业务代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
批量格式转换服务
提供Excel转PDF、Excel转图片、Excel转Word等格式转换功能
"""
import os

from api.excel_api import ExcelAPI
from api.news.excel_api_new import ExcelAPINews


class FormatConvertService:
    """格式转换服务类"""

    def __init__(self):
        self.excel_api = ExcelAPINews()

    def handle(self , input_path , output_path , args):
        convert_type = args['convert_type']
        out_dir = args['out_dirs']
        base_name = os.path.splitext(os.path.basename(output_path))[0]
        if convert_type == "excel_to_pdf":
            output_file = os.path.join(out_dir, base_name + ".pdf")
            self.excel_api.excel_to_pdf(input_path, output_file , {})
        elif convert_type == "excel_to_image":
            output_file = os.path.join(out_dir, base_name + ".png")
            self.excel_api.excel_to_image(input_path, output_file , {})
        elif convert_type == "excel_to_word":
            output_file = os.path.join(out_dir, base_name + ".docx")
            self.excel_api.excel_to_word(input_path, output_file , {})

        pass

    def get_excel_files(self, directory):
        """获取目录下的所有Excel文件"""
        excel_files = []
        for root, dirs, files in os.walk(directory):
            # 跳过out.error目录
            if "out" in dirs:
                dirs.remove("out")
            if "error" in dirs:
                dirs.remove("error")
            for file in files:
                if file.startswith("~$"):
                    continue
                if file.startswith("~"):
                    continue
                if file.startswith("~wr"):
                    continue
                if file.lower().endswith(('.xlsx', '.xls')):
                    excel_files.append(os.path.join(root, file))
        return excel_files

    def get_word_files(self, directory):
        """获取目录下的所有Excel文件"""
        excel_files = []
        for root, dirs, files in os.walk(directory):
            # 跳过out.error目录
            if "out" in dirs:
                dirs.remove("out")
            if "error" in dirs:
                dirs.remove("error")
            for file in files:
                if file.startswith("~$"):
                    continue
                if file.startswith("~"):
                    continue
                if file.startswith("~wr"):
                    continue
                if file.lower().endswith(('.docx', '.doc')):
                    excel_files.append(os.path.join(root, file))
        return excel_files

    def convert_excel_to_pdf(self, excel_file, output_file):
        """将Excel转换为PDF"""
        self.excel_api.excel_to_pdf(excel_file, output_file)

    def convert_excel_to_image(self, excel_file, output_file):
        """将Excel转换为图片"""
        self.excel_api.excel_to_png(excel_file, output_file)

    def convert_excel_to_word(self, excel_file, output_file):
        """将Excel转换为Word"""
        self.excel_api.excel_to_word(excel_file, output_file)

    def convert_word_to_excel(self, excel_file, output_file):
        self.excel_api.word_to_excel(excel_file, output_file)

    def convert_word_to_pdf(self, excel_file, output_file):
        self.excel_api.word_to_pdf(excel_file, output_file)

    def convert_word_to_png(self, excel_file, output_file):
        self.excel_api.word_to_png(excel_file, output_file)

代码没有开源噢。 如果您有技术合作意向,还请联系本人。今天就介绍到 这里 ,希望能帮助大家,感谢!!!

结尾语

多文件批量一键处理还可以尝试我文章中的介绍方法,可以为你提高很大的工作效率,让你有时间充实自己,而不是像机器人一样做重复的工作,没有任何新的收获。 就说到这里了, 如帮助到你了,还请点个赞,感谢!!