分类
Programming

Python Excel 和 Json 互转

一、Excel 转 Json

import pandas
pandas.read_excel("Data.xlsx", sheet_name=0).to_json("1.json", orient="records", force_ascii=False)

二、Json 转 Excel

import pandas
pandas.read_json("input.json",dtype=str).to_excel("output.xlsx")
分类
Programming

Python 批量按文件名关键字新建文件夹并归档文件

按需求修改规则使用,现在代码的规则对应的文件名为:xxx-xxx-xxx-xxx-xxx-xxx.jpg。


import os
import shutil

current_path = os.getcwd()
print('当前目录:'+current_path)

filename_list = os.listdir(current_path)
print('当前目录下文件:',filename_list)

print('正在分类整理进文件夹ing...')
for filename in filename_list:
    try:
        name1, name2, name3, name4, name5, name6 = filename.split('-') //规则
        dirName = name1 + "-" + name2 + "-" + name3 + "-" + name4 + "-" + name5 //规则
        try:
            os.mkdir(dirName)
            print('创建文件夹:' + dirName)
        except:
            pass
        try:
            shutil.move(current_path+'/'+filename, current_path+'/'+dirName)
            print(filename+'转移成功!')
        except Exception as e:
            print('移动失败:' + e)
    except:
        pass

print('整理完毕!')
分类
Windows

Windows 批量将子目录文件移动到根目录 bat

放到包含一堆子文件夹的文件根目录运行。

@echo off
for /f "delims=" %%a in ('dir /a-d /b /s ') do (move "%%~a" ./)
分类
Programming

Python 批量分割裁剪图片

放到图片目录下运行,以下代码为从中间将图片一分为二,请根据自己需要修改。

from PIL import Image
import os

path = os.getcwd()
print('当前目录:'+path)
path_list = os.listdir(path)
print(path_list)

j=1

for i in path_list:
    a = open(os.path.join(path,i),'rb')
    img = Image.open(a)
    w = img.width
    h = img.height
    print('正在处理图片',i,'宽',w,'长',h)

    box1 = (0,0,w*0.5,h) 
    img1 = img.crop(box1)
    box2 = (w*0.5,0,w,h)
    img2 = img.crop(box2)

    img1.save(str(j)+'.jpg', dpi=img1.info["dpi"])
    j = j + 1
    img2.save(str(j)+'.jpg', dpi=img2.info["dpi"])
    j = j + 1
分类
Programming

Python 批量旋转图片

放到图片目录下运行。

from PIL import Image
import os

path = os.getcwd()
print('当前目录:'+path)
path_list = os.listdir(path)
print(path_list)

for i in path_list:
    a = open(os.path.join(path,i),'rb')
    img = Image.open(a)
    w = img.width
    h = img.height
    print('正在处理图片',i,'宽',w,'长',h)
    if h > w:
        img.rotate(180, expand=True).save('0'+i, dpi=img.info["dpi"])
        print('旋转成功')
    
print("'{}'目录下所有图片已经保存到本文件目录下。".format(path))