使用python修改docx


import os
import datetime
import time
from docx import Document
from docx.shared import Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH


from PIL import Image, ImageGrab
import PIL
import subprocess
x = subprocess.run(r'D:\User\Desktop\snipaste\Snipaste.exe snip')

im = ImageGrab.grabclipboard()
if isinstance(im, Image.Image):
    print("Image: size : %s, mode: %s" % (im.size, im.mode))
    im.save(r"D:\User\Desktop\snipaste\grab_clipboard.jpg")






file_path = r"D:\User\Desktop\绑定更新文档.docx"
context = '创建文件'
all_picture = [r"D:\User\Pictures\Snipaste_2023-10-31_13-28-53.png", r"D:\User\Pictures\Snipaste_2023-10-31_13-28-53.png"]

if os.path.exists(file_path):
    document=Document(file_path)
else:
    document=Document()

index = len(document.paragraphs) + 1
date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

paragraph = document.add_paragraph('{}. {}\n{}\n'.format(index, date, context))

for picture in all_picture:
    paragraph.runs[-1].add_picture(picture, height=Cm(4))
    paragraph.runs[-1].add_text('\n')
paragraph.runs[-1].add_text('\r')

document.save(file_path)


评论
  目录