3

Python 的 word 占位符批量插入图片,新手..不知道代码哪里出问题

 2 years ago
source link: https://www.v2ex.com/t/849234
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Python

Python 的 word 占位符批量插入图片,新手..不知道代码哪里出问题

  jbue520 · 7 小时 40 分钟前 · 274 次点击
from docx import  Document
from docx.shared import Inches
import glob
from os import listdir
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPH


def center_insert_img(doc,img):
    """插入图片"""
    for paragraph in doc.paragraphs:
        # 根据文档中占位符定位图片插入的位置
        if '<<img1>>' in paragraph.text:
            paragraph.text = paragraph.text.replace('<<img1>>', '')
            run = paragraph.add_run('')
            run.add_break()

            run.add_picture(img, width=Inches(6.2))


def save_img_to_doc(img):
    tpl_doc = r'D:\py\归档模版.docx'
    res_doc = r'D:\py\空港.docx'

    document = Document(tpl_doc)

    center_insert_img(document,img)

    document.save(res_doc)


def main():
    path = r'C:\Users\durpa\Desktop\身份证\*.jpg'
    # img = 'C:\\Users\\durpa\\Desktop\\身份证\\75+'
    for img in glob.glob(path):
        print(img)



    save_img_to_doc(img)


if __name__ == '__main__':
    main()

代码能运行,但是只能插入一张图...还请大神指教一下..感谢


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK