吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4019|回复: 30
上一主题 下一主题
收起左侧

[Python 转载] EXCEL的xls和xlsx转换工具 已打包为EXE 提供源码

[复制链接]
跳转到指定楼层
楼主
xx10060 发表于 2023-7-27 09:31 回帖奖励
本帖最后由 xx10060 于 2023-8-18 11:47 编辑

由于工作需要,在论坛中找到excel的转换工具,原帖https://www.52pojie.cn/forum.php?mod=viewthread&tid=1686635&highlight=xlsx%2B%D7%AA%2Bxls

可能是因为时间比较久了,工具不好用,根据作者提供的代码改造了一下。
重新打包了EXE,下载地址:https://wwmc.lanzoub.com/iSyow13ldcbc
现在我也把代码分享给大家,感兴趣的小伙伴可以继续改造,打包源码 EXCEL.zip (1.4 KB, 下载次数: 111)

[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from datetime import datetime
import PySimpleGUI as sg
import win32com.client as win32
import os
import sys
 
excel = win32.gencache.EnsureDispatch('Excel.Application')
 
def convert_file(file_name, output_format):
    input_ext = os.path.splitext(file_name)[-1].lower()
    if input_ext not in ['.xlsx', '.xls']:
        return f'{file_name} 不支持的文件类型'
    elif input_ext == output_format:
        return f'{file_name} 当前文件类型无需转换'
    try:
        wb = excel.Workbooks.Open(file_name)
 
        # 保存转换后的文件
        new_file_name = os.path.splitext(file_name)[0] + output_format
        # 替换文件路径中的斜杠
        new_file_name = new_file_name.replace("/", "\\")
        wb.SaveAs(new_file_name, FileFormat=get_format(output_format))
        wb.Close()
 
        return f'转换成功:{file_name} 转换为 {output_format}'
    except Exception as e:
        excel.Application.Quit()
        return f'【转换失败】:{file_name}'
 
def get_format(file_ext):
    if file_ext == '.xls':
        return 56
    elif file_ext == '.xlsx':
        return 51
 
def gui():
    sg.theme('DarkBlue')
 
    # 更换程序图标的文件路径
    icon_path = './icon1.ico'
 
    layout = [
        [sg.Text('文件格式转换工具', font=('黑体', 32), justification='center', text_color='#00CED1')],
        [sg.Text('选择要转换的文件', font=('黑体', 16), text_color='#00CED1')],
        [sg.Input(key='文件路径', size=(40, 1), font=('黑体', 12)),
         sg.FilesBrowse('选择', file_types=(("Excel文件", "*.xlsx; *.xls"),), target='文件路径',
                        font=('黑体', 12), button_color=('white', '#00CED1'))],
        [sg.Radio('xlsx转xls', '选择转换格式', key='xlsx_to_xls', default=True, font=('黑体', 14), enable_events=True),
         sg.Radio('xls转xlsx', '选择转换格式', key='xls_to_xlsx', font=('黑体', 14), enable_events=True)],
        [sg.Button('转换', key='转换', button_color=('white', '#00CED1'), size=(10, 1), font=('黑体', 14)),
         sg.Button('清空', key='清空', size=(10, 1), font=('黑体', 14))],
        [sg.Output(size=(60, 10), key='sg_output', font=('黑体', 12))]
    ]
 
    window = sg.Window('文件格式转换工具.By.Eric.', layout, element_justification='center', resizable=True,
                       finalize=True, icon=icon_path)
 
    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            excel.Application.Quit()
            break
        if event == '转换':
            output_format = '.xls' if values['xlsx_to_xls'] else '.xlsx'
            for i in values['文件路径'].split(';'):
                print(convert_file(i, output_format))
                excel.Application.Quit()
            window['文件路径'].update('')
        elif event == '清空':
            window['文件路径'].update('')
            window.FindElement('sg_output').Update("")
 
    window.close()
 
if __name__ == '__main__':
    gui()

20230727093448_rec_.gif (1 MB, 下载次数: 7)

演示

演示

免费评分

参与人数 3吾爱币 +2 热心值 +2 收起 理由
greatywh666 + 1 + 1 谢谢@Thanks!
bin_chb + 1 谢谢@Thanks!
FAFA + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
lea999 发表于 2024-1-5 17:44
D:\anaconda3\python.exe C:\Users\Administrator\Desktop\原创力文档下载工具\EXCEL\EXCEL.py
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\原创力文档下载工具\EXCEL\EXCEL.py", line 2, in <module>
    import PySimpleGUI as sg
ModuleNotFoundError: No module named 'PySimpleGUI'

进程已结束,退出代码1
推荐
 楼主| xx10060 发表于 2023-7-29 14:00 |楼主
wapjsx 发表于 2023-7-27 17:07
请问下大佬,为什么我不能直接运行python代码呢?

这部分代码是需要有依赖库的,估计你那些引用的依赖没有导致报错的,你可以用python的开发工具去运行这段代码,工具会帮你下载本地没有的依赖库
3#
wapjsx 发表于 2023-7-27 17:07
请问下大佬,为什么我不能直接运行python代码呢?
4#
Dark_Forest 发表于 2023-7-28 09:48
wapjsx 发表于 2023-7-27 17:07
请问下大佬,为什么我不能直接运行python代码呢?

报错信息发一下
5#
ywd683 发表于 2023-7-31 06:31
谢谢大佬分享。
6#
bin_chb 发表于 2023-8-3 13:07
wapjsx 发表于 2023-7-27 17:07
请问下大佬,为什么我不能直接运行python代码呢?

第一行datetime后边少了个回车
7#
zjm2013 发表于 2023-8-3 19:23
研究一下,谢谢
8#
 楼主| xx10060 发表于 2023-8-18 11:48 |楼主
bin_chb 发表于 2023-8-3 13:07
第一行datetime后边少了个回车

感谢指正,已修改
9#
ethonjohn 发表于 2023-11-19 11:25
请问是不是只能在win10以上运行,我的系统是win7,提示找不到相应的模块
10#
yp5768 发表于 2023-12-4 12:10

这个厉害了,谢谢楼主的分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-15 21:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表