Skip to content

Commit

Permalink
#119 finished sample draw code.
Browse files Browse the repository at this point in the history
  • Loading branch information
toru-ver4 committed Sep 19, 2020
1 parent a2570e5 commit ca3ae15
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 2 deletions.
122 changes: 122 additions & 0 deletions 2020/027_excel_control_by_python/excel_control_sample_draw_bitmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# -*- coding: utf-8 -*-
"""
sample code for excel control using python
==========================================
* draw Gawr Gura's twitter banner
"""

# import standard libraries
import os

# import third-party libraries
import cv2
import numpy as np

# import my libraries
import excel_control_utility as ecu

# banner https://twitter.com/gawrgura
IMG_DATA = "./img/1500x500.jpg"


def conv_rgb_8bit_color_to_excel_24bit_format(rgb):
"""
convert from 8bit rgb array to 24bit color value for excel.
Parameters
----------
rgb : ndarray
8 bit image data.
Returns
-------
ndarray
24bit color values for excel.
Examples
--------
>>> img = np.array(
... [[[255, 0, 0], [0, 255, 0]],
... [[0, 0, 255], [64, 128, 192]]])
>>> conv_rgb_8bit_color_to_excel_24bit_format(rgb=img)
"""
# return r * 0x00000001 + g * 0x00000100 + b * 0x00010000
return rgb[..., 0] * 0x00000001 + np.uint32(rgb[..., 1]) * 0x0000100\
+ rgb[..., 2] * 0x00010000


def load_8bit_image(fname=IMG_DATA):
img = cv2.imread(IMG_DATA)[..., ::-1]

return img


def crop_and_resize_image(img):
img_crop = img[:, 327:327+1045, :]
scale_factor = 3
after_width = img_crop.shape[1] // scale_factor
after_height = img_crop.shape[0] // scale_factor
out_img = cv2.resize(
img_crop, dsize=(after_width, after_height))

return out_img


def draw_banner(ws):
"""
draw the twitter banner on the Excel.
Parameters
----------
ws : win32com.client.CDispatch
excel worksheet
"""
# 画像の準備
img_org = load_8bit_image(fname=IMG_DATA)
img = crop_and_resize_image(img_org)
img_width = img.shape[1]
img_height = img.shape[0]
img_color = conv_rgb_8bit_color_to_excel_24bit_format(img)

# セルが正方形となるようにサイズ変更
ecu.change_col_width(ws, 0.5, st_pos_col=0, col_num=img_width)
ecu.change_row_height(ws, 5.5, st_pos_row=0, row_num=img_height)

# 描画 (Interior.Color は配列アクセスできなかった🥺🥺🥺)
for row_idx in range(img_height):
for col_idx in range(img_width):
ws.Cells(row_idx + 1, col_idx + 1).Interior.Color\
= img_color[row_idx, col_idx]


def main_func():
# エクセルアプリ起動
excel_app = ecu.launch_excel_app()

# 最大化
ecu.maximize_excel_window(excel_app)

# エクセルファイル新規作成
wb = ecu.create_excel_file(excel_app=excel_app)

# シート1 に 1次元の塗りつぶしを実行
ws = wb.Worksheets(1)
ws.Name = "DRAW"
draw_banner(ws)

# 保存
ecu.save_excel_file(
filename="./draw_sample.xlsx", wb=wb, excel_app=excel_app)

# # # ファイルを閉じる
# ecu.close_excel_file(wb)

# # # エクセルアプリの終了
# ecu.quit_excel_app(excel_app=excel_app)


if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.abspath(__file__)))
main_func()
12 changes: 10 additions & 2 deletions 2020/027_excel_control_by_python/excel_control_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ def get_range(ws, st_pos=(0, 0), row_num=4, col_num=8):
-------
???
An Excel Range
Examples
--------
>>> row_num = 4
>>> col_num = 8
>>> cell_range = ecu.get_range(
... ws, st_pos=(2, 1), row_num=row_num, col_num=col_num)
>>> data = np.arange(row_num * col_num).reshape((row_num, col_num))
>>> cell_range.Value = data
"""
st_cell = get_cell(ws, pos=st_pos)
ed_cell = get_cell(ws, pos=(st_pos[0] + row_num - 1, st_pos[1] + col_num - 1))
Expand All @@ -301,7 +310,7 @@ def change_row_height(ws, height, st_pos_row, row_num):
height : int
height of the raw.
st_pos_row : int
start position of row.
start position of row. 0 is the start coordinate.
row_num : int
number of row.
"""
Expand All @@ -325,7 +334,6 @@ def change_col_width(ws, width, st_pos_col, col_num):
col_num : int
number of columns.
"""
# range = f"{st_pos_col}:{ed_pos_col - 1}"
cell_range = get_range(
ws, st_pos=(0, st_pos_col), col_num=col_num)
cell_range.ColumnWidth = width
Expand Down

0 comments on commit ca3ae15

Please sign in to comment.