Spaces:
Sleeping
Sleeping
Upload app.py
Browse files1672 unmodified lines167316741675167616771678167916801681168216835 unmodified lines16891690169116921693169416951696169716981699248 unmodified lines194819491950195119521953125 unmodified lines20792080208120822083208436 unmodified lines21212122212321242125212621273 unmodified lines2131213221332134213521361672 unmodified linesfrom pptx.dml.color import RGBColorreturn RGBColor.from_string(hex_str.lstrip("#").upper())def _pp_fill_text(tf, lines, size, bold=False, color="#111827", align="c", anchor="m", margins=(0, 0, 0, 0)):"""把文字寫進 text_frame(每行一個段落)。align: l/c/r;anchor: t/m/b;margins 為英吋 (左, 上, 右, 下)。"""from pptx.util import Pt, Inchesfrom pptx.enum.text import PP_ALIGN, MSO_ANCHORtf.word_wrap = Falsetf.margin_left, tf.margin_top, tf.margin_right, tf.margin_bottom = (Inches(m) for m in margins)tf.vertical_anchor = {"t": MSO_ANCHOR.TOP, "m": MSO_ANCHOR.MIDDLE, "b": MSO_ANCHOR.BOTTOM}[anchor]lines = [lines] if isinstance(lines, str) else list(lines)5 unmodified linesrun.font.size, run.font.bold, run.font.name = Pt(size), bold, PPT_FONTrun.font.color.rgb = _pp_rgb(color)def _pp_text(shapes, x, y, w, h, lines, size, bold=False, color="#111827", align="c", anchor="m", name=None):from pptx.util import Inchestb = shapes.add_textbox(Inches(x), Inches(y), Inches(w), Inches(h))if name: tb.name = name_pp_fill_text(tb.text_frame, lines, size, bold, color, align, anchor)return tbdef _pp_shape(shapes, kind, x, y, w, h, fill=None, line=None, line_pt=1.0, name=None, radius=0.5):248 unmodified linespt = row.geometry.representative_point()items.append({"name": str(row["label_name"]), "full": str(row["name"]), "lines": lines,"lon": pt.x, "lat": pt.y})items = items[:max(int(max_items), 1)]for it in items:it["x"], it["y"] = to_slide([(it["lon"], it["lat"])])[0]125 unmodified lines"y": top + (len(title_lines) * title_pt * 1.26 / 72 + 0.13 if title_lines else 0),"w": ANNO_BOX_W, "h": len(sub_lines) * sub_pt * 1.42 / 72},"note": {"text": note, "x": ANNO_PAD, "y": bottom + 0.12, "w": ANNO_W - 2 * ANNO_PAD, "pt": 8.5} if note else None,"bottom": bottom, "fit": fit, "pt": pt}36 unmodified linesfill="#FFFFFF", line=_hex_mix(main, "#FFFFFF", 0.62), line_pt=0.75, name=f"說明框 {b['name']}")_pp_text(slide.shapes, b["x"] + 0.11, b["y"] + b["header"] + 0.03 + 0.04,ANNO_BOX_W - 0.22, b["body"] - 0.06, b["lines"], b["pt"], False, "#1F2937", "l", "t",name=f"說明 {b['name']}")t = plan["title"]if t["lines"]:3 unmodified linesif s["lines"]:_pp_text(slide.shapes, s["x"], s["y"], s["w"], s["h"], s["lines"], s["pt"], True, main, "l", "t",name="副標")n = plan["note"]if n:_pp_line(slide.shapes, (n["x"], n["y"] - 0.04), (n["x"] + n["w"], n["y"] - 0.04), "#E5E7EB", 0.75)1672 unmodified lines1673167416751676167716781679168016811682168316845 unmodified lines16901691169216931694169516961697169816991700248 unmodified lines1949195019511952195319541955125 unmodified lines208120822083208420852086208736 unmodified lines21242125212621272128212921303 unmodified lines21342135213621372138213921402141214221431672 unmodified linesfrom pptx.dml.color import RGBColorreturn RGBColor.from_string(hex_str.lstrip("#").upper())def _pp_fill_text(tf, lines, size, bold=False, color="#111827", align="c", anchor="m", margins=(0, 0, 0, 0), wrap=False):"""把文字寫進 text_frame(每行一個段落)。align: l/c/r;anchor: t/m/b;margins 為英吋 (左, 上, 右, 下)。wrap=True 時交給 PowerPoint 自動換行(字寬估算略偏時會自己折,不會衝出框外)。"""from pptx.util import Pt, Inchesfrom pptx.enum.text import PP_ALIGN, MSO_ANCHORtf.word_wrap = bool(wrap)tf.margin_left, tf.margin_top, tf.margin_right, tf.margin_bottom = (Inches(m) for m in margins)tf.vertical_anchor = {"t": MSO_ANCHOR.TOP, "m": MSO_ANCHOR.MIDDLE, "b": MSO_ANCHOR.BOTTOM}[anchor]lines = [lines] if isinstance(lines, str) else list(lines)5 unmodified linesrun.font.size, run.font.bold, run.font.name = Pt(size), bold, PPT_FONTrun.font.color.rgb = _pp_rgb(color)def _pp_text(shapes, x, y, w, h, lines, size, bold=False, color="#111827", align="c", anchor="m", name=None, wrap=False):from pptx.util import Inchestb = shapes.add_textbox(Inches(x), Inches(y), Inches(w), Inches(h))if name: tb.name = name_pp_fill_text(tb.text_frame, lines, size, bold, color, align, anchor, wrap=wrap)return tbdef _pp_shape(shapes, kind, x, y, w, h, fill=None, line=None, line_pt=1.0, name=None, radius=0.5):248 unmodified linespt = row.geometry.representative_point()items.append({"name": str(row["label_name"]), "full": str(row["name"]), "lines": lines,"lon": pt.x, "lat": pt.y})dropped = max(0, len(items) - max(int(max_items), 1))items = items[:max(int(max_items), 1)]for it in items:it["x"], it["y"] = to_slide([(it["lon"], it["lat"])])[0]125 unmodified lines"y": top + (len(title_lines) * title_pt * 1.26 / 72 + 0.13 if title_lines else 0),"w": ANNO_BOX_W, "h": len(sub_lines) * sub_pt * 1.42 / 72},"note": {"text": note, "x": ANNO_PAD, "y": bottom + 0.12, "w": ANNO_W - 2 * ANNO_PAD, "pt": 8.5} if note else None,"dropped": dropped, "max_items": int(max_items),"bottom": bottom, "fit": fit, "pt": pt}36 unmodified linesfill="#FFFFFF", line=_hex_mix(main, "#FFFFFF", 0.62), line_pt=0.75, name=f"說明框 {b['name']}")_pp_text(slide.shapes, b["x"] + 0.11, b["y"] + b["header"] + 0.03 + 0.04,ANNO_BOX_W - 0.22, b["body"] - 0.06, b["lines"], b["pt"], False, "#1F2937", "l", "t",name=f"說明 {b['name']}", wrap=True)t = plan["title"]if t["lines"]:3 unmodified linesif s["lines"]:_pp_text(slide.shapes, s["x"], s["y"], s["w"], s["h"], s["lines"], s["pt"], True, main, "l", "t",name="副標")if plan.get("dropped"):_pp_text(slide.shapes, ANNO_W - ANNO_PAD - 4.2, plan["bottom"] + 0.12, 4.2, 0.24,f"另有 {plan['dropped']} 個地區未顯示(最多 {plan['max_items']} 個)", 8.5, False, "#9CA3AF", "r", "t",name="省略提示")n = plan["note"]if n:_pp_line(slide.shapes, (n["x"], n["y"] - 0.04), (n["x"] + n["w"], n["y"] - 0.04), "#E5E7EB", 0.75)
app.py+13-6