from pathlib import Path

from docx import Document


SRC = Path("/Users/hedy/Desktop/软著-智慧食堂称重结算系统V1.0/智慧食堂称重结算系统_软件边界修订版.docx")
OUT = Path("/Users/hedy/Desktop/软著-智慧食堂称重结算系统V1.0/智慧食堂称重结算系统_软件边界修订去设备图版.docx")


def has_picture(paragraph):
    return bool(paragraph._p.xpath(".//w:drawing")) or bool(paragraph._p.xpath(".//pic:pic"))


def remove_paragraph(paragraph):
    element = paragraph._element
    parent = element.getparent()
    parent.remove(element)
    paragraph._p = paragraph._element = None


def main():
    doc = Document(SRC)
    removed = False
    for paragraph in doc.paragraphs:
        if paragraph.text.strip() == "" and has_picture(paragraph):
            remove_paragraph(paragraph)
            removed = True
            break
    if not removed:
        raise RuntimeError("No standalone picture paragraph found to remove.")
    doc.save(OUT)
    print(OUT)


if __name__ == "__main__":
    main()
