Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #### Requirements
- #
- # - python3 (tested on 3.5)
- # - openpyxl
- # - Pillow
- # - progressbar2
- #
- #### Usage
- #
- # $ ./excelizer.py image.jpg target.xlsx [--size max_width] [--cell-scale N]
- #
- # Where cell-scale set cell width as in cell-base*cell-scale (cell-base is different for width and height)
- #
- #### Potential improvements
- #
- # - named styles?
- # - pararell processing?
- #
- from openpyxl import Workbook
- from openpyxl.styles import PatternFill
- from openpyxl.utils.cell import get_column_letter
- from PIL import Image
- from argparse import ArgumentParser
- from progressbar import ProgressBar
- parser = ArgumentParser()
- parser.add_argument('image')
- parser.add_argument('target')
- parser.add_argument('-s', '--size', default=250, type=int)
- parser.add_argument('-c', '--cell-scale', default=2, type=int)
- args = parser.parse_args()
- size = args.size
- im = Image.open(args.image)
- im.thumbnail((size, size))
- w, h = im.size
- wb = Workbook()
- ws = wb.create_sheet("Image", 0)
- for i in range(0, w):
- ws.column_dimensions[get_column_letter(i+1)].width = 2*args.cell_scale
- for i in range(0, h):
- ws.row_dimensions[i+1].height = 8*args.cell_scale
- bar = ProgressBar(max_value=w*h)
- for x in range(0, w):
- letter = get_column_letter(x+1)
- for y in range(0, h):
- c = im.getpixel((x, y))
- color = "FF%.2X%.2X%.2X" % (c[0],c[1],c[2])
- coord = "%s%d" % (letter, y+1)
- style = PatternFill("solid", fgColor=color, bgColor=color)
- ws[coord].fill = style
- bar.update(x * h + y+1)
- wb.save(args.target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement