1. Hiroki MIZUNO
  2. blockdiag

Commits

Hiroki MIZUNO  committed a15c20a

add Excel Formatter: only rectange, line, textarea, polygon supported

  • Participants
  • Parent commits 4bccda0
  • Branches default

Comments (0)

Files changed (3)

File src/blockdiag/imagedraw/__init__.py

View file
-# -*- coding: utf-8 -*-
-#  Copyright 2011 Takeshi KOMIYA
-#
-#  Licensed under the Apache License, Version 2.0 (the "License");
-#  you may not use this file except in compliance with the License.
+# -*- coding: utf-8 -*- #  Copyright 2011 Takeshi KOMIYA # #  Licensed under the Apache License, Version 2.0 (the "License"); #  you may not use this file except in compliance with the License.
 #  You may obtain a copy of the License at
 #
 #      http://www.apache.org/licenses/LICENSE-2.0
 except ImportError:
     pass
 
+from excel import ExcelImageDraw
+drawers['xls'] = ExcelImageDraw
 
 def create(format, filename, size, **kwargs):
+    print format
     if format.lower() in drawers:
         return drawers[format.lower()](filename, size, **kwargs)
     else:

File src/blockdiag/imagedraw/excel.py

View file
+# -*- coding: utf-8 -*- #  Copyright 2011 Takeshi KOMIYA # #  Licensed under the Apache License, Version 2.0 (the "License"); #  you may not use this file except in compliance with the License.  #  You may obtain a copy of the License at # #      http://www.apache.org/licenses/LICENSE-2.0 # #  Unless required by applicable law or agreed to in writing, software #  distributed under the License is distributed on an "AS IS" BASIS, #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  #  See the License for the specific language governing permissions and #  limitations under the License.
+
+import re
+import base64
+from blockdiag.utils import textfolder, urlutil, Box, XY
+import win32com.client
+import os
+import os.path
+
+class ExcelImageDraw(object):
+    self_generative_methods = []
+    def __init__(self, filename, size, **kwargs):
+        self.excel = win32com.client.Dispatch("Excel.Application") 
+        self.excel.Visible=True
+#        self.DisplayAlerts=False #�x�����b�Z�[�W��OFF
+        self.book = self.excel.Workbooks.Add()
+
+    def path(self, pd, **kwargs):
+        pass
+
+    def rgb(self, rgb):
+        r,g,b = rgb
+        return (r << 16) + (g << 8) + b
+
+    def rectangle(self, box, **kwargs):
+        left = box[0]
+        top  = box[1]
+        width = box[2] - box[0]
+        height = box[3]  - box[1]
+
+        fill = kwargs.get('fill', 'none')
+        outline = kwargs.get('outline')
+
+        shape = self.book.ActiveSheet.Shapes.AddShape(1, left, top, width, height)
+        shape.Line.ForeColor.RGB = self.rgb(outline)
+
+        if fill != "none":
+            shape.Fill.ForeColor.RGB = self.rgb(fill)
+
+    def text(self, xy, string, font, **kwargs):
+        print "text"
+
+    def textarea(self, box, string, font, **kwargs):
+        left = box[0]
+        top  = box[1]
+        width = box[2] - box[0]
+        height = box[3]  - box[1]
+
+        fill = kwargs.get('fill', 'none')
+        shape = self.book.ActiveSheet.Shapes.AddShape(1, left, top, width, height)
+        shape.Line.Visible = 0
+
+        shape.Fill.ForeColor.RGB = 0xFFFFFF
+        chars = shape.TextFrame.Characters()
+        chars.Text = string
+        chars.Font.Color = self.rgb(fill)
+        chars.Font.Size = font.size
+
+        if kwargs['halign'] == 'center':
+            shape.TextFrame.HorizontalAlignment = -4108
+        shape.TextFrame.VerticalAlignment = -4108
+
+    def line(self, xy, **kwargs):
+        x1,y1 = xy[0]
+        x2,y2 = xy[1]
+        self.book.ActiveSheet.Shapes.AddLine(x1, y1, x2, y2)
+
+    def arc(self, xy, start, end, **kwargs):
+        print "arc"
+
+    def ellipse(self, xy, **kwargs):
+        print "eclipse"
+
+    def polygon(self, xy, **kwargs):
+        fill = kwargs.get('fill', 'none')
+        outline = kwargs.get('outline')
+
+        form = self.book.ActiveSheet.Shapes.BuildFreeform(0, xy[0][0], xy[0][1])
+
+        for (x,y) in xy[1:]:
+            form.AddNodes(0, 0, x, y) 
+        shape = form.ConvertToShape()
+        shape.Line.ForeColor.RGB = self.rgb(outline)
+
+        if fill != "none":
+            shape.Fill.ForeColor.RGB = self.rgb(fill)
+
+
+    def loadImage(self, filename, box):
+        pass
+
+    def save(self, filename, size, format):
+        path = os.path.abspath(filename)
+        if os.path.exists(path):
+            os.remove(path)
+        self.book.SaveAs(path, 56)
+        self.excel.Quit()

File src/blockdiag/utils/bootstrap.py

View file
-# -*- coding: utf-8 -*-
-#  Copyright 2011 Takeshi KOMIYA
-#
+# -*- coding: utf-8 -*- #  Copyright 2011 Takeshi KOMIYA #
 #  Licensed under the Apache License, Version 2.0 (the "License");
 #  you may not use this file except in compliance with the License.
 #  You may obtain a copy of the License at
             self.options.output = basename + ext
 
         self.options.type = self.options.type.upper()
-        if not self.options.type in ('SVG', 'PNG', 'PDF'):
+        if not self.options.type in ('SVG', 'PNG', 'PDF','XLS'):
             msg = "unknown format: %s" % self.options.type
             raise RuntimeError(msg)