目录
前言
由于近期经常写报告,开始的时候是把报告写一个汇总文档里,后来要按标题一个一个的拆分,为了方便,特此写了个脚本来拆分
代码实现
import os
from docx import Document
from docx.shared import Inches
from docx.shared import Pt
from docx.shared import Cm
#创建一个函数,将文档中所有的图片保存到images文件夹下,图片名以图片的embed属性值命名(即 rId 命名)
def extract_and_save_images_from_docx(doc_path):
# 加载文档
doc = Document(doc_path)
# 创建用于存储图片的文件夹
image_folder = 'images'
if not os.path.exists(image_folder):
os.makedirs(image_folder)
# 获取文档中的所有图片
def find_images(document):
images = []
rels = document.part.rels
for rel in rels.values():
if 'image' in rel.reltype:
image_part = rel.target_part
embed_id = rel.rId
images.