Skip to content

Instantly share code, notes, and snippets.

@tk0miya
Created November 5, 2012 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tk0miya/4014800 to your computer and use it in GitHub Desktop.
Save tk0miya/4014800 to your computer and use it in GitHub Desktop.
xaxtsuxo_to_excel.py
[buildout]
parts = app
[app]
recipe = zc.recipe.egg
eggs =
PIL
openpyxl
interpreter = py
# -*- coding:utf-8 -*-
# Copyright (c) 2010-2011 openpyxl
# Copyright (c) 2012 @yamada_program
#
# http://yamada-program.blogspot.jp/2012/10/python-openpyxl.html
import openpyxl
def write_worksheet_sheetviews(doc, worksheet):
from openpyxl.writer.worksheet import start_tag, end_tag, tag
from openpyxl.cell import coordinate_from_string
from openpyxl.worksheet import column_index_from_string
viewattrs = {'workbookViewId': '0'}
#その他ビューパラメータを取得
sheetview = worksheet.sheet_view
if sheetview:
if hasattr(sheetview, 'zoomScale'):
viewattrs['zoomScale'] = sheetview.zoomScale
if hasattr(sheetview, 'zoomScaleNormal'):
viewattrs['zoomScaleNormal'] = sheetview.zoomScaleNormal
start_tag(doc, 'sheetViews')
start_tag(doc, 'sheetView', viewattrs)
selectionAttrs = {}
topLeftCell = worksheet.freeze_panes
if topLeftCell:
colName, row = coordinate_from_string(topLeftCell)
column = column_index_from_string(colName)
pane = 'topRight'
paneAttrs = {}
if column > 1:
paneAttrs['xSplit'] = str(column - 1)
if row > 1:
paneAttrs['ySplit'] = str(row - 1)
pane = 'bottomLeft'
if column > 1:
pane = 'bottomRight'
paneAttrs.update(dict(topLeftCell=topLeftCell,
activePane=pane,
state='frozen'))
tag(doc, 'pane', paneAttrs)
selectionAttrs['pane'] = pane
if row > 1 and column > 1:
tag(doc, 'selection', {'pane': 'topRight'})
tag(doc, 'selection', {'pane': 'bottomLeft'})
selectionAttrs.update({'activeCell': worksheet.active_cell,
'sqref': worksheet.selected_cell})
tag(doc, 'selection', selectionAttrs)
end_tag(doc, 'sheetView')
end_tag(doc, 'sheetViews')
# 既定のwriterメソッドを置き換え
openpyxl.writer.worksheet.write_worksheet_sheetviews = write_worksheet_sheetviews
#!bin/py
import Image
from urllib import urlopen
from StringIO import StringIO
import openpyxl
from openpyxl.style import Color, Fill
from openpyxl.cell import get_column_letter
import sys
sys.path.insert(0, '.')
import patch # add zooming feature to openpyxl
def load_image(url):
return Image.open(StringIO(urlopen(url).read()))
def image_to_pixels(image):
_, height = image.size
for i, color in enumerate(image.getdata()):
x = i % height
y = i / height
yield (x, y), color
class Canvas(object):
def __init__(self, image):
self.book = openpyxl.Workbook()
self.sheet = self.book.worksheets[0]
# setup default zoom of sheet
sheet_view = openpyxl.worksheet.SheetView()
sheet_view.zoomScale = "25"
sheet_view.zoomScaleNormal = "25"
self.sheet.sheet_view = sheet_view
# setup sheet as HOGAN
coldim = openpyxl.worksheet.ColumnDimension()
coldim.width = 2
for x in range(1, image.size[0] + 1):
colname = get_column_letter(x)
self.sheet.column_dimensions[colname] = coldim
rowdim = openpyxl.worksheet.RowDimension()
rowdim.height = 12
for y in range(1, image.size[1] + 1):
self.sheet.row_dimensions[y] = rowdim
self.sheet.cell(row=y - 1, column=1).value = ' '
def put_pixel(self, pt, color):
color = "00%02X%02X%02X" % color
cell = self.sheet.cell(column=pt[0], row=pt[1])
cell.style.fill.fill_type = Fill.FILL_SOLID
cell.style.fill.start_color = Color(color)
cell.style.fill.end_color = Color(color)
def save_as(self, filename):
self.book.save(filename)
def main():
url = 'https://si0.twimg.com/profile_images/1133570329/dog.jpg'
image = load_image(url)
canvas = Canvas(image)
for xy, color in image_to_pixels(image):
canvas.put_pixel(xy, color)
canvas.save_as('xaxtsuxo.xlsx')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment