#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""在v7中追加刘子研11月W1周报页；不新建版本号。"""
import os
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.enum.shapes import MSO_SHAPE
from pptx.oxml.ns import qn
from lxml import etree

ROOT = '/Users/liuxinyuan/Desktop/Hermes输出-工作类'
SRC = os.path.join(ROOT, '经营分析/月报/2026年10月销售部工作汇报_草稿_20261108_v7.pptx')
LOGO = '/Users/liuxinyuan/work/公司资料/logo_transparent.png'

NAVY = RGBColor(0x0B, 0x2A, 0x4A)
BLUE = RGBColor(0x1F, 0x6F, 0xEB)
LIGHT = RGBColor(0xEA, 0xF2, 0xFB)
PALE = RGBColor(0xF5, 0xF8, 0xFC)
GREY = RGBColor(0x5B, 0x6B, 0x7F)
MIDGREY = RGBColor(0x8A, 0x99, 0xAA)
RED = RGBColor(0xC0, 0x39, 0x2B)
WHITE = RGBColor(0xFF, 0xFF, 0xFF)


def set_run(run, size=8.3, color=NAVY, bold=False):
    run.font.size = Pt(size)
    run.font.bold = bold
    run.font.color.rgb = color
    run.font.name = 'Times New Roman'
    rPr = run._r.get_or_add_rPr()
    ea = rPr.find(qn('a:ea'))
    if ea is None:
        ea = etree.SubElement(rPr, qn('a:ea'))
    ea.set('typeface', '宋体')
    cs = rPr.find(qn('a:cs'))
    if cs is None:
        cs = etree.SubElement(rPr, qn('a:cs'))
    cs.set('typeface', '宋体')


def add_text(slide, x, y, w, h, text, size=9, color=NAVY, bold=False,
             align=PP_ALIGN.LEFT, anchor=MSO_ANCHOR.TOP, line_spacing=1.08):
    box = slide.shapes.add_textbox(Inches(x), Inches(y), Inches(w), Inches(h))
    tf = box.text_frame
    tf.word_wrap = True
    tf.margin_left = tf.margin_right = tf.margin_top = tf.margin_bottom = 0
    tf.vertical_anchor = anchor
    for i, line in enumerate(str(text).split('\n')):
        p = tf.paragraphs[0] if i == 0 else tf.add_paragraph()
        p.alignment = align
        p.line_spacing = line_spacing
        r = p.add_run(); r.text = line
        set_run(r, size, color, bold)


def add_rect(slide, x, y, w, h, fill, line=None, radius=False):
    shp = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE if radius else MSO_SHAPE.RECTANGLE,
                                 Inches(x), Inches(y), Inches(w), Inches(h))
    shp.fill.solid(); shp.fill.fore_color.rgb = fill
    if line:
        shp.line.color.rgb = line; shp.line.width = Pt(0.75)
    else:
        shp.line.fill.background()
    if radius:
        try:
            shp.adjustments[0] = 0.08
        except Exception:
            pass


prs = Presentation(SRC)
blank = prs.slide_layouts[1]
s = prs.slides.add_slide(blank)
for shape in list(s.shapes):
    if shape.is_placeholder:
        shape._element.getparent().remove(shape._element)

add_rect(s, 0, 0, 13.333, 0.06, BLUE)
if os.path.exists(LOGO):
    s.shapes.add_picture(LOGO, Inches(0.48), Inches(0.22), height=Inches(0.34))
add_text(s, 0.48, 0.60, 12.0, 0.42, '11月W1重点进展与新增卡点｜刘子研', size=18, color=NAVY, bold=True)
add_text(s, 0.48, 1.03, 12.3, 0.28,
         '提炼自 11月W1周报｜只列影响 Q4 目标/客户决策的事项，日常测试不单列',
         size=9.2, color=GREY)

rows = [
    ['之江\n登革热',
     '003006 比 092401 表现好，客户定版 003006；合同 2.5万T，11/1 发 2万T，11/4 发 100T 小样，小样通过后发剩余 5000T。',
     '已从测试转入批量交付；小样结果决定剩余 5000T。',
     '跟进 100T 小样结果；确保 5000T 同批次交付；记录 E 完成。',
     '刘子研 / 11/30'],
    ['科华\n血筛',
     'DNA 血筛 M4211-3 希望做单管，性能不突出但看稳定性/抗抑制；Tth 血筛 3 个靶标用 3 款 Tth 均不起线，内部决定改引探。',
     'Tth 体系不匹配，科华要求今年内敲定；M4211-3 资料未齐。',
     '发 M4211-3 COA/MSDS/稳定性；Tth 改引探后重测；争取年内定体系。',
     '刘子研 / 技术支持\n11/30'],
    ['西安天隆\nATP + Tth',
     '王新旺确认血筛 Tth 两管/1.5h/2–8℃ 18个月；李红东 ATP 测试意愿低，认为现有 Biolum 成熟，资料不足以比较系统 LOD/精密度/稳定性。',
     'ATP 缺对标数据，短期难送测；Tth 需求明确但需匹配。',
     '1–2 个月补 Biolum 对标数据；推进 Tth 两管/三管方案测试。',
     '刘子研 / 技术支持\n12/31'],
    ['佰奥莱博\n冻干',
     'FMD5084-000901、FM5144 冻干后性能下降；阳性检出率降低、IC 阴性率升高；客户要 CAPA；贴标大小标签不一致。',
     '性能+标签双问题，客户质量关注度上升。',
     '出 CAPA；统一标签；补酶量方案验证；跟踪客户检测报告。',
     '刘子研 / 质量\n11/30'],
    ['天隆\n003006',
     '003006 已替换 ZJ4；下周发 2000T 838/003006 冻干球；需拉客户线上会确定 003006 出货检测标准。',
     '出货标准未双方互认，影响后续批量。',
     '拉会定标准；跟进 2000T 发货/测试；整理货号清单。',
     '刘子研 / 技术支持\n11/30'],
    ['丹威 / 艾德 / 华益美',
     '丹威：LAMP 5天加速有非特异、10天正常；艾德/华益美：下周追资料，暂无新测试结果。',
     '丹威需排查假阳/污染；艾德/华益美资料未齐。',
     '丹威查假阳来源；艾德/华益美追资料并约测试。',
     '刘子研 / 11/30'],
]

headers = ['客户/项目', 'W1 进展', '判断/卡点', '11月动作', '责任人/时间']
widths = [1.45, 4.15, 2.75, 2.95, 1.10]
nrows, ncols = len(rows) + 1, 5
table = s.shapes.add_table(nrows, ncols, Inches(0.48), Inches(1.45), Inches(12.4), Inches(4.95)).table
table.first_row = True
table.horz_banding = False
for j, wd in enumerate(widths):
    table.columns[j].width = Inches(wd)
table.rows[0].height = Inches(0.40)
for i in range(1, nrows):
    table.rows[i].height = Inches(0.66)
for j, htext in enumerate(headers):
    cell = table.cell(0, j)
    cell.fill.solid(); cell.fill.fore_color.rgb = NAVY
    cell.margin_left = Inches(0.05); cell.margin_right = Inches(0.04)
    cell.margin_top = Inches(0.02); cell.margin_bottom = Inches(0.02)
    cell.vertical_anchor = MSO_ANCHOR.MIDDLE
    p = cell.text_frame.paragraphs[0]
    p.alignment = PP_ALIGN.CENTER if j in (0, 4) else PP_ALIGN.LEFT
    r = p.add_run(); r.text = htext
    set_run(r, 8.4, WHITE, True)

for i, row in enumerate(rows, start=1):
    fill = PALE if i % 2 == 0 else WHITE
    for j, val in enumerate(row):
        cell = table.cell(i, j)
        cell.fill.solid(); cell.fill.fore_color.rgb = fill
        cell.margin_left = Inches(0.05); cell.margin_right = Inches(0.04)
        cell.margin_top = Inches(0.02); cell.margin_bottom = Inches(0.02)
        cell.vertical_anchor = MSO_ANCHOR.MIDDLE
        p = cell.text_frame.paragraphs[0]
        p.alignment = PP_ALIGN.CENTER if j in (0, 4) else PP_ALIGN.LEFT
        p.line_spacing = 1.02
        r = p.add_run(); r.text = str(val)
        color = RED if j == 2 else NAVY
        set_run(r, 7.9, color, j == 0)

add_rect(s, 0.48, 6.55, 12.4, 0.38, LIGHT, line=RGBColor(0xD4, 0xE2, 0xF4), radius=True)
add_text(s, 0.62, 6.63, 12.0, 0.2,
         '未单列：丹威/艾德/华益美的日常测试与资料追补；已放在本页最后一行，按周报继续跟踪。',
         size=7.2, color=GREY)
add_rect(s, 0.48, 7.02, 12.35, 0.012, RGBColor(0xD9, 0xE2, 0xEE))
add_text(s, 0.48, 7.10, 9.9, 0.22, '来源：销售工程师周报-刘子研（2026年11月8日）；10月月报承接页。', size=7.1, color=MIDGREY)
add_text(s, 10.55, 7.10, 2.25, 0.22, '守护生命健康，缔造美好生活', size=7.5, color=BLUE, bold=True, align=PP_ALIGN.RIGHT)

# insert after P18 (the existing W1 赵云浩 page), index 18
sldIdLst = prs.slides._sldIdLst
new_id = sldIdLst[-1]
sldIdLst.remove(new_id)
sldIdLst.insert(18, new_id)

prs.save(SRC)
print('saved in place', SRC)
print('slides', len(prs.slides))
