网上看到很多都是使用prettytable库绘制的表格,但是很不美观,所以自己使用pillow库绘制一个。
Python生成可设置单元格背景色的表格图片
一、安装pillow
Python版本使用Python3.8
pip3 install pillow
二、代码实现
为了简化功能,代码中很多参数固定写死,可以根据需要自己来调整。
#!/bin/python3
# -*- coding:UTF-8 -*-
from PIL import Image, ImageDraw, ImageFont
import csv
def create_table_img(data, table_data_color, image_name, **kwargs):
# 图片类型
image_type = "JPEG"
# 图片背景底色
table_image_background_color = (255, 255, 255)
# 图片高宽
if 'table_image_width' not in kwargs:
table_image_width = 1000
else:
table_image_width = kwargs['table_image_width']
if 'table_image_height' not in kwargs:
table_image_height = 600
else:
table_image_height = kwargs['table_image_height']
if 'font' not in kwargs:
kwargs['font'] = None
else:
font = kwargs['font']
# 表格水平垂直间距
table_ver_space = 10
table_hor_space = 10
# 字体大小设置
table_title_font_size = 30
table_data_font_size = 18
table_desc_font_size = 16
font_table_title = ImageFont.truetype(
font, table_title_font_size, encoding='utf-8')
font_table_data = ImageFont.truetype(
font, table_data_font_size, encoding='utf-8')
font_table_desc = ImageFont.truetype(
font, table_desc_font_size, encoding='utf-8')
# 表格数据高宽
if 'table_data_width' not in kwargs:
table_data_width = 160
else:
table_data_width = kwargs['table_data_width']
if 'table_data_height' not in kwargs:
table_data_height = 30
else:
table_data_height = kwargs['table_data_height']
# Image模块创建一个图片对象
im = Image.new('RGB', (table_image_width, table_image_height),
table_image_background_color)
# ImageDraw向图片中进行操作
draw = ImageDraw.Draw(im)
# 文字都居中显示
table_top_offset = 0
# draw title
if 'table_title' in kwargs:
table_title = kwargs['table_title']
txt_title_size = draw.textsize(table_title, font=font_table_title)
# 居中显示
draw.text((table_image_width/2 - txt_title_size[0]/2, table_ver_space),
table_title, fill=(0, 0, 0), font=font_table_title)
table_top_offset = table_ver_space * 2 + txt_title_size[1]
# draw data
table_column_count = len(data[0])
table_row_count = len(data)
for i in range(0, table_row_count):
for j in range(0, table_column_count):
x = table_data_width*j + table_hor_space
y = table_data_height*i + table_top_offset
draw.rectangle((x, y, x + table_data_width, y + table_data_height),
table_data_color[i][j], (0, 0, 0, 255))
txt_size = draw.textsize(data[i][j], font=font_table_data)
draw.text((x+table_data_width/2-txt_size[0]/2, y+table_data_height /
2-txt_size[1]/2), data[i][j], fill=(0, 0, 0), font=font_table_data)
# draw desc
if 'table_desc' in kwargs:
table_desc = kwargs['table_desc']
draw.text((table_hor_space, table_data_height*table_row_count + table_top_offset +
table_ver_space), table_desc, fill=(0, 0, 0), font=font_table_desc)
del draw
# 保存为图片
im.save(image_name, image_type)
return True
if __name__ == "__main__":
data_result = []
os_platform = "windows" # windows/linux
# os_platform = "linux" #windows/linux
if os_platform == "windows":
csv_path = "E:\\data\\result.csv"
reslut_path = "E:\\data\\"
font_path = "C:\\Windows\\Fonts\\simhei.ttf"
else:
csv_path = "/home/data/result.csv"
reslut_path = "/home/data/"
font_path = "/usr/share/fonts/chinese/simhei.ttf"
img_name = "result.jpg"
with open(csv_path, 'r', encoding='utf-8') as fp:
for i in csv.reader(fp):
i[0] = i[0].replace('\ufeff', '')
data_result.append(i)
metric = [{
"name": "数学",
"position": 1,
"value_pass": 90
}, {
"name": "语文",
"position": 2,
"value_pass": 90
}, {
"name": "英语",
"position": 3,
"value_pass": 90
}, {
"name": "理综",
"position": 4,
"value_pass": 180
}]
data = data_result.copy()
img_path = reslut_path + img_name
table_title = "学生考试成绩结果表"
table_desc = "注:表格中标注红色底色的成绩表示不合格,无颜色标注的成绩表示合格。"
table_column_count = len(data[0])
table_row_count = len(data)
table_data_color = [[(255, 255, 255) for i in range(
table_column_count)] for j in range(table_row_count)]
index = 0
for element in metric:
index += 1
element_pos = element['position']
element_value_pass = element['value_pass']
for i in range(1, table_row_count-1):
value = data_result[i]
if (float(value[element_pos]) - element_value_pass) <= 0:
table_data_color[i][element_pos] = (255, 0, 0)
result = create_table_img(data, table_data_color, img_path,
font=font_path,
table_title=table_title,
table_desc=table_desc,
table_image_width=820,
table_image_height=450,
table_data_width=160,
table_data_height=30)
三、测试结果
1、数据文件示例:
姓名,数学,语文,英语,理综
张一,100,100,100,280
张二,100,100,100,280
张三,100,80,100,280
张四,100,100,100,280
张五,100,100,100,70
张六,100,100,100,280
张七,60,100,100,280
赵一,100,100,100,280
赵二,100,50,100,280
赵三,100,100,100,280
赵四,100,100,100,280
2、测试结果