随着工作量的增加,越来越多的数据需要处理。其中不乏格式转化。有的时候需要处理的数据会出现几十上百个,如果一个一个的进行格式转化,效率太低。因此我就想能不能写一个批量化进行格式转化的程序。
于是今天的代码就诞生了。
注: 下面提供的代码只是一个框架,用于实现批量化的数据处理。具体的格式转化代码可自行添加和补充。这里我只写了 nii转nii.gz 、 webm转mp4 、 ppt转pdf 的代码作为示例和个人使用。
后续会进行持续更新
按照我的设想这只是一个批量化处理数据的一个框架,格式转化只是批量化处理的一个最基础的想法。
框架功能
这里我结合我的日常经验为我的程序设计了一些人性化的功能。
自动遍历目标格式所在的位置
例:
-数据/
|- 1/
|- - 1.mp4
|- - 2.mp4
|- 3.mp4
我们子需要规定最外层目录的位置Data,并指定MP4的文件,它就可以自己找到data里面全部的mp4文件。无论它嵌套了几层。(除非级文件夹套文件夹,加上你内存太小把你的内存撑爆了。应该不会有人靠这个栈溢出了吧!😅)
保留原文件的文件结构
以上面为例
如果选择保留,我们把mp4文件转为mp3文件输出结果就如下:
-输出/
|- 1/
|- - 1.mp3
|- - 2.mp3
|- 3.mp3
如果不保留结果就如下;
-输出/
|- 1.mp3
|- 2.mp3
|- 3.mp3
多线程功能
因为格式转换涉及大量的读取与写入,会消耗大量的时间用于io读写。因此加入多线程提高效率。
代码部分
主题代码
import os
from concurrent.futures import ThreadPoolExecutor
from .readFile import readDirToList
from pathlib import Path
import sys
def BatchChanceData(input_dir: str, output_dir: str, mode: int | str, dir_keep: bool = True, thread_number=4) -> None:
'''
用于批量格式转换
参数:
input_dir: 输入文件的主目录
output_dir: 输出文件的目录
mode: 转换的模式
dir_keep: 是否保留原始目录结构
thread_number: 线程数量
'''
# 判断输入值是否符合要求
if not os.path.isdir(input_dir) or not os.path.isdir(output_dir):
raise FileNotFoundError('输入路径或者输出路径有误')
modes = [1, 'nii2nii.gz', 2, 'webm2mp4',3,'pptx2pdf']
if mode not in modes:
raise TabError('模式不存在')
input_dir = str(Path(input_dir))
output_dir = str(Path(output_dir))
# 创建线程池
pool = ThreadPoolExecutor(thread_number)
# 获得需要转化的文件路径
match mode:
case 1 | 'nii2nii.gz':
input_type = '.nii'
output_type = '.nii.gz'
case 2 | 'webm2mp4':
input_type = '.webm'
output_type = '.mp4'
case 3 | 'pptx2pdf' :
input_type = '.pptx'
output_type = '.pdf'
print('获取文件中')
file_path_list = readDirToList(input_dir, input_type)
print('获取到的文件有:')
print('-'*10)
for file_path in file_path_list:
print(file_path)
print('-'*10)
# 开始格式转换
futures = []
for file_path in file_path_list:
# 获得输出路径
file_path = str(Path(file_path))
file_name = os.path.basename(file_path)
new_file_name = file_name.replace(input_type, output_type)
if dir_keep:
new_file_path = file_path.replace(
input_dir, output_dir, 1)
new_file_path = new_file_path.replace(
file_name, new_file_name, -1)
if not os.path.isdir(os.path.dirname(new_file_path)):
os.mkdir(os.path.dirname(new_file_path))
else:
new_file_path = os.path.join(output_dir, new_file_name)
# 开始转换
future = pool.submit(_chanceData, file_path, new_file_path, mode)
futures.append(future)
pass
# 设置线程守护
pool.shutdown(wait=True)
return
def _chanceData(input_file_path, output, mode):
'''
用于进行单个文件的转换
参数:
input_file_path: 输入文件的地址
output: 输出文件的地址
mode: 转换模式
'''
# 根据转换模式选择转换方法
try:
print(f'正在转换文件{input_file_path}')
match mode:
case 1 | 'nii2nii.gz':
chanceDate = __nii2niigz
case 2 | 'webm2mp4':
chanceDate = __webm2mp4
case 3 | 'pptx2pdf' :
chanceDate = __ppt2pdf
# 转换
chanceDate(input_file_path, output)
print(f'{output}转换完成')
except Exception as e:
print(e)
pass
使用示例:
BatchChanceData('./test/data', r'./test/output/mp4/',3, dir_keep=假, thread_number=4)
nii转nii.gz格式代码
def __nii2niigz(input_file_path, output):
'''
将nii文件转化为nii.gz文件
'''
# 读取文件
from .readFile import readNii
data = readNii(input_file_path)
# 保存文件
from .writeDataToFile import writeNiigz
writeNiigz(data, output)
pass
def writeNiigz(data, output):
'''
保存文件
'''
import SimpleITK as sitk
sitk.WriteImage(data, output)
pass
def readNii(file_path: str):
'''
用于读取nii文件
不能包含中文路径
'''
import SimpleITK as sitk
data = sitk.ReadImage(file_path)
return data
webm转mp4代码
def __webm2mp4(input_file_path, output):
'''
将webm文件转化为mp4文件
'''
from moviepy.editor import VideoFileClip
# 读取文件
video = VideoFileClip(input_file_path)
video.write_videofile(output)
pass
ppt转pdf代码
def __ppt2pdf(input_file_path, output):
'''
将ppt文件转化为pdf文件
'''
from .readFile import readPptx
from .writeDataToFile import writePdf
data = readPptx(input_file_path)
writePdf(data,output)
pass
def readPptx(file_path: str):
from pptx import Presentation
return Presentation(file_path)
pass
def writePdf(data, output):
import pdfkit
presentation = data
# 暂存每一张幻灯片的内容
html_content = ''
for slide_index, slide in enumerate(presentation.slides):
html_content += f'<h1>Slide {slide_index + 1}</h1>'
for shape in slide.shapes:
if hasattr(shape, "text"):
html_content += f'<p>{shape.text}</p>'
# 保存为 HTML 文件
html_file_path = 'temp_slide.html'
with open(html_file_path, 'w', encoding='utf-8') as f:
f.write(html_content)
# 使用 pdfkit 将 HTML 转换为 PDF
pdfkit.from_file(html_file_path, output)
pass
github链接
这里我附上这个项目的链接,欢迎大家拉取使用: dream520nb/dataChance: 批量化数据格式转化