吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1630|回复: 12
收起左侧

[Python 原创] 批量将wps转txt

[复制链接]
fryant 发表于 2023-5-17 21:25
利用Python批量将wps转txt
直接展示代码吧~
[Python] 纯文本查看 复制代码
import os
import chardet

# 自定义文件路径
file_path = r"C:\Users\Fryant\Desktop\data\policy\wps"

# 遍历指定目录下的所有文件
for root, dirs, files in os.walk(file_path):
    for file in files:
        # 判断文件是否是WPS文件
        if file.endswith(".wps"):
            # 打开WPS文件,并读取其中的内容
            with open(os.path.join(root, file), "rb") as f:
                content = f.read()

            # 检测文件的编码格式
            encoding = chardet.detect(content)["encoding"]

            # 将文件内容转化为字符串
            content_str = content.decode(encoding)

            # 将文件内容保存到txt文件中
            with open(os.path.join(root, file[:-4] + ".txt"), "w", encoding="utf-8") as f:
                f.write(content_str)
print('处理完成')

免费评分

参与人数 3吾爱币 +7 热心值 +2 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Varg + 1 + 1 谢谢@Thanks!
Valar_Dohearis + 1 我很赞同!

查看全部评分

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

TR小米哥 发表于 2023-5-22 16:29

看起来没有明显的错误或不足,但是可以进行一些改进和优化。

1.在处理大量文件时,最好使用多线程或异步方式,以提高程序的效率。

2.在处理文件时,最好使用上下文管理器(with语句),以确保文件在使用完毕后能够被正确关闭。

3.在检测文件编码格式时,最好使用第三方库chardet2,它的准确度比chardet更高。

4.在保存文件内容到txt文件中时,最好使用上下文管理器,并指定文件编码格式。同时,可以考虑使用os.path.splitext()函数来获取文件名和扩展名,以避免硬编码。

import os
import chardet2
import concurrent.futures

# 自定义文件路径
file_path = r"C:\Users\Fryant\Desktop\data\policy\wps"

def process_file(file):
    # 判断文件是否是WPS文件
    if file.endswith(".wps"):
        # 打开WPS文件,并读取其中的内容
        with open(file, "rb") as f:
            content = f.read()

        # 检测文件的编码格式
        encoding = chardet2.detect(content)["encoding"]

        # 将文件内容转化为字符串
        content_str = content.decode(encoding)

        # 将文件内容保存到txt文件中
        with open(os.path.splitext(file)[0] + ".txt", "w", encoding="utf-8") as f:
            f.write(content_str)

# 使用多线程处理文件
with concurrent.futures.ThreadPoolExecutor() as executor:
    # 遍历指定目录下的所有文件
    for root, dirs, files in os.walk(file_path):
        # 处理每个文件
        for file in files:
            file = os.path.join(root, file)
            executor.submit(process_file, file)

print('处理完成')
龍謹 发表于 2023-5-18 06:55
pdfhvy141 发表于 2023-5-18 07:29
wobzhidao 发表于 2023-5-18 08:30
这个好,省了不少事。
mac666 发表于 2023-5-18 08:33
感谢分享
Easonll 发表于 2023-5-18 08:49
图片也能转吗
头像被屏蔽
yulinsoft 发表于 2023-5-18 08:49
提示: 作者被禁止或删除 内容自动屏蔽
lingwushexi 发表于 2023-5-18 09:01
感谢分享,学习学习
 楼主| fryant 发表于 2023-5-19 14:56
yulinsoft 发表于 2023-5-18 08:49
不能用,
Traceback (most recent call last):
  File "C:%users\liwei\Desktop\wps2txt.py", line 20, i ...

把encoding换成'utf-8','ascii'或者'latin-1'试试哦~
 楼主| fryant 发表于 2023-5-19 14:57

不支持图片的哦~
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

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

GMT+8, 2024-4-19 15:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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