Spaces:
Sleeping
Sleeping
Download app.py from Overtime62178/Draw_Excel: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/spaces/Overtime62178/Draw_Excel/resolve/main/app.py
- Command line
-
hf download hf://spaces/Overtime62178/Draw_Excel/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Overtime62178/Draw_Excel/resolve/main/app.py
3.23 kB
| import gradio as gr | |
| from PIL import Image, ImageDraw, ImageFont | |
| import csv, io | |
| def generate_techflow_table(csv_file, background_image, font_size, row_height): | |
| # 1. 载入背景图 | |
| bg = Image.open(background_image).convert("RGBA") | |
| bg_w, bg_h = bg.size | |
| draw = ImageDraw.Draw(bg) | |
| # 2. 加载字体 | |
| try: | |
| font_title = ImageFont.truetype("PingFang.ttc", font_size + 6) | |
| font_body = ImageFont.truetype("PingFang.ttc", font_size) | |
| except: | |
| font_title = ImageFont.load_default() | |
| font_body = ImageFont.load_default() | |
| # 3. 读取 CSV 内容 | |
| csv_data = csv_file.decode('utf-8').splitlines() | |
| reader = csv.reader(csv_data) | |
| rows = list(reader) | |
| headers = rows[0] | |
| data = rows[1:] | |
| # 4. 表格布局参数 | |
| start_x, start_y = 100, 230 | |
| col_spacing = 60 | |
| # 计算列宽 | |
| col_widths = [] | |
| for c in range(len(headers)): | |
| max_width = max(draw.textlength(r[c], font=font_body) for r in rows) | |
| col_widths.append(max_width + col_spacing) | |
| # 5. 绘制标题 | |
| title_text = "隐私币近期表现" | |
| title_font = ImageFont.truetype("PingFang.ttc", font_size + 10) | |
| draw.text((start_x, 130), title_text, font=title_font, fill="white") | |
| # 6. 绘制表头背景条 | |
| header_y = start_y | |
| table_width = sum(col_widths) | |
| draw.rectangle( | |
| [start_x - 20, header_y - 10, start_x + table_width, header_y + row_height], | |
| fill=(30, 50, 120, 200) | |
| ) | |
| # 7. 绘制表头 | |
| for i, text in enumerate(headers): | |
| x = start_x + sum(col_widths[:i]) | |
| draw.text((x, header_y), text, font=font_body, fill="white") | |
| # 8. 绘制表格内容 | |
| for r, row in enumerate(data): | |
| y = start_y + (r + 1) * row_height | |
| for c, text in enumerate(row): | |
| x = start_x + sum(col_widths[:c]) | |
| draw.text((x, y), text, font=font_body, fill="white") | |
| # 9. 绘制表格边界线(可选) | |
| y_end = start_y + (len(data) + 1) * row_height | |
| draw.line([start_x - 20, y_end, start_x + table_width, y_end], fill=(200, 200, 200, 100), width=2) | |
| # 10. 加上 logo(右下角) | |
| try: | |
| logo = Image.open("logo.png").convert("RGBA") | |
| logo_w, logo_h = logo.size | |
| scale = 0.25 | |
| logo = logo.resize((int(logo_w * scale), int(logo_h * scale))) | |
| pos = (bg_w - logo.width - 80, bg_h - logo.height - 60) | |
| bg.paste(logo, pos, logo) | |
| except: | |
| pass # 如果没有 logo 文件则忽略 | |
| # 11. 输出图片 | |
| output = io.BytesIO() | |
| bg.save(output, format="PNG") | |
| output.seek(0) | |
| return output | |
| # Gradio 界面 | |
| demo = gr.Interface( | |
| fn=generate_techflow_table, | |
| inputs=[ | |
| gr.File(label="上传 CSV 文件(UTF-8)"), | |
| gr.Image(label="背景图", type="filepath", value="background.png"), | |
| gr.Slider(20, 60, value=36, step=2, label="字体大小"), | |
| gr.Slider(60, 140, value=100, step=5, label="行距"), | |
| ], | |
| outputs=gr.Image(label="生成结果", type="pil"), | |
| title="📊 TechFlow 深潮风格表格生成器", | |
| description="上传 CSV 文件,自动生成品牌风格的表格图片(背景与字体自动排版)" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |