end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

openpyxl + pillow for python で excelに含まれる画像を抽出

import openpyxl
import PIL

def main():
    excel_file = './test_includeing_img.xlsx'
    wb = openpyxl.load_workbook(excel_file)

    for xlsx_img in wb['Sheet1']._images:
        #print( xlsx_img.path )
        
        # sheet中の座標
        print( xlsx_img.anchor._from.col, xlsx_img.anchor._from.row )

        pil_img = PIL.Image.open( xlsx_img.ref )

        pil_img = pil_img.resize( ( pil_img.width//2, pil_img.height//2 ) )

        output_path = "./test_extract_img.png"
        pil_img.save(output_path)
        
if __name__ == '__main__':
    main()