咔片PPT · AI自动生成演示文稿,模板丰富、排版精美 讯飞智文 · 一键生成PPT和Word,高效应对学习与办公

有时候我们会遇到在word里面取出图片这种任务。要是我们一个个复制出来呢。会很麻烦。

那么我们用Python帮助我们进行抽取word中存在的图片,提高工作效率

import docx import os,re def get_picture(wordpath,resultpath): doc=docx.Document(wordpath)#Python读取doc文件 print(doc) dict_rel=doc.part._rels print(dict_rel) for rel in dict_rel: rel=dict_rel[rel] print(rel) print(rel.target_ref) if 'image' in rel.target_ref: if not os.path.exists(resultpath): os.makedirs(resultpath) img_name=re.findall("/(.*)",rel.target_ref)[0] print(img_name) word_name=os.path.splitext(wordpath)[0] print(os.path.splitext(wordpath)) print(os.sep) if os.sep in word_name: new_name=word_name.split('{}'.format(''))[-1] print(new_name) else: new_name=word_name.split('/')[-1] img_name=f'{new_name}_{img_name}' print(img_name) with open(f"{resultpath}/{img_name}",'wb')as f: f.write(rel.target_part.blob) # print(rel) if __name__ == '__main__': word=r"C:UsersyellowDesktopsamxxxx.docx"#word的路径 result=r"C:UsersyellowDesktopsamwordpicresult"#文件夹的路径 get_picture(word,result)

Python还可以读取word中存在的表格,段落。还是很强的。因此在获取数据时,可以用Python的方式。