openpyxl 在excel里添加网址中的图片

modules

  • python version: 2.7
  • openpyxl version: 2.4.8
  • 需要用到PIL模块,但PIL不维护了,用Pillow 代替 pip install Pillow.
    Ref: https://stackoverflow.com/questions/20060096/installing-pil-with-pip

核心代码:

    img_url='http://pic.qqtn.com/up/2016-10/14762726301049405.jpg'
    wb = openpyxl.Workbook()
    ws = wb.active
    # setup row height & col width
    ws.row_dimensions[1].height =152
    ws.column_dimensions['A'].width =25
    # fetch image
    resp = requests.get(img_url)
    img_file = io.BytesIO(resp.content)
    img = Image(img_file)
    ws.add_image(img,'A1')
    wb.save('excel-image.xlsx')

source code

你可能感兴趣的:(openpyxl 在excel里添加网址中的图片)