python 将电子签名的图片插入到word,实现签字效果

代码

import os
from docx import Document
from docx.shared import Inches


# 将图片插入word表格
def insert_signature_in_table(doc, search_text, image_path, width=Inches(1.0), height=Inches(0.5)):
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                # Check each paragraph in the cell
                for paragraph in cell.paragraphs:
                    if search_text in paragraph.text:
                        # Split the paragraph into two parts and add the signature image in between
                        text_parts = paragraph.text.split(search_text)
                        paragraph.clear()  # Clear the original paragraph

                        # Add the first part of the text back to the paragraph
                        paragraph.add_run(text_parts[0])

                        # Add the search_text back as it was
                        paragraph.add_run(search_text)

                        # Insert the image after the specified text
                        run = paragraph.add_run()
                        run.add_picture(image_path, width=width, height=height)

                        # Add the remaining part of the text if any
                        if len(text_parts) > 1:
                            paragraph.add_run(text_parts[1])

                        return True  # Assuming there's only one occurrence of the search_text
    return False  # If the search_text is not found in any table


# 将图片插入word段落
def insert_signature_after_text(doc, search_text, image_path, width=Inches(1.0), height=Inches(0.5)):
    for paragraph in doc.paragraphs:
        if search_text in paragraph.text:
            # Split the paragraph into two parts and add the signature image in between
            text_parts = paragraph.text.split(search_text)
            paragraph.clear()  # Clear the original paragraph

            # Add the first part of the text back to the paragraph
            paragraph.add_run(text_parts[0])

            # Add the search_text back as it was
            paragraph.add_run(search_text)

            # Insert the image after the specified text
            run = paragraph.add_run()
            run.add_picture(image_path, width=width, height=height)

            # Add the remaining part of the text if any
            if len(text_parts) > 1:
                paragraph.add_run(text_parts[1])

            return True  # Assuming there's only one occurrence of the search_text
    return False


def signature_main():
    dst_docx = r'D:\ljh\signature\signature.docx'
    img = r"D:\ljh\signature\signature.png"

    # Load an existing document
    doc = Document(dst_docx)

    # Call the function to insert the signature image
    # if insert_signature_in_table(doc, "主任签名", img, Inches(1.0), Inches(0.5)):
    if insert_signature_after_text(doc, "教师签名", img, Inches(1.0), Inches(0.5)):
        print("签名已成功插入")
    else:
        print("未找到签名文本")
    # Save changes to the document
    doc.save(dst_docx)


if __name__ == '__main__':
    signature_main()

插入签名图片后顺带写入日期:
# 将图片插入word表格, 并写入日期
def insert_signature_in_table(doc_file, search_text, image_path, width=Inches(1.0), height=Inches(0.5)):
    is_signature = False
    doc = Document(doc_file)
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                is_signature_name = False
                is_signature_time = False
                for paragraph in cell.paragraphs:
                    if not is_signature_name and search_text in paragraph.text:
                        text_parts = paragraph.text.split(search_text)
                        paragraph.clear()

                        paragraph.add_run(text_parts[0])

                        paragraph.add_run(search_text)

                        run = paragraph.add_run()
                        run.add_picture(image_path, width=width, height=height)

                        is_signature_name = True

                    if is_signature_name and not is_signature_time and "年月日" in paragraph.text.strip().replace(" ", ""):
                        _now = datetime.datetime.now()
                        text_parts = paragraph.text.split("年")
                        paragraph.clear()
                        old_y = text_parts[0].strip().replace(" ", "")
                        now_y = _now.strftime("%Y")
                        paragraph.add_run("{}年".format(text_parts[0].replace(old_y, str(now_y))))
                        now_m = _now.strftime("%m")
                        now_d = _now.strftime("%d")
                        paragraph.add_run(" {}月 {}日".format(now_m, now_d))
                        is_signature_time = True
                    if is_signature_name:
                        is_signature = True
    if is_signature:
        doc.save(doc_file)
        return True

    return False

实现效果

表格

table

段落

text

签名后并写入日期

docx

posted @ 2025-01-13 15:15  二月雪  阅读(67)  评论(0)    收藏  举报