math98

(自编自用,记性不好,记录一下)

import xlsxwriter, os
from PIL import Image

def getFiles(dir, suffix): # 查找根目录,文件后缀
    res = []
    for root, directory, files in os.walk(dir): # =>当前根,根下目录,目录下的文件
        for filename in files:
            name, suf = os.path.splitext(filename) # =>文件名,文件后缀
            if suf.lower() == suffix.lower():
                res.append(os.path.join(root, filename)) # =>把一串字符串组合成路径
    return res
# for file in getFiles(\'D:/Python/downtest/my_pic\', \'.png\'): # =>查找以.py结尾的文件
def main():
    workbook = xlsxwriter.Workbook(\'image_scaled.xlsx\')
    worksheet = workbook.add_worksheet()
    # image_width = 90.0
    # image_height = 160.0

    cell_width = 18.0
    cell_height = 32.0

    files = getFiles(os.getcwd(), \'.jpg\')
    for index in range(len(files)):
        img = Image.open(files[index])
        w = img.width  # 图片的宽
        h = img.height  # 图片的高
        x_scale = 10*cell_width / w
        y_scale = 10*cell_height / h
        num = index%3
        if num==0:
            clm = \'B\'
        elif num==1:
            clm = \'E\'
        else:
            clm = \'H\'
        row = (index//3)*18 +3
        worksheet.insert_image(clm+str(row), files[index],
                               {\'x_scale\': x_scale, \'y_scale\': y_scale})
    workbook.close()

if __name__ == \'__main__\':
    main()

 

分类:

技术点:

相关文章: