使用python-docx包提取word文档里的图片

安装python-docx包,pip install python-docx

import docx
import re

i = 0
t = -1
r = -1
c = -1
old_picname = ''
doc = docx.Document(r"实验表.docx")
# blob =  doc.tables[0].rows[16].cells[0].part.rels['rId5'].target_part.blob
for table in doc.tables:
    t = t+1
    for row in table.rows:
        r = r + 1
        for cell in row.cells:
            c = c+1
            xml = cell.paragraphs[0]._p.xml
            if 'a:blip r:embed=' in xml:
                picname = doc.tables[t].rows[r+1].cells[c].text
                if old_picname != picname:
                    old_picname = picname
                else:
                    continue
                results = re.findall(r'a:blip r:embed="(.+?)"',xml)
                print(results[0] +"-"+ str(t)+"-"+str(r)+"-"+str(c))
                with open(r"C:\Users\tom\Desktop\%s.png"%(str(t)+"-"+str(r)+"-"+str(c)), "wb") as f:
                    i = i+1
                    f.write(doc.part.rels[results[0]].target_part.blob)
        c = -1
    r = -1
Subscribe
提醒
guest

0 评论
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
交流思想,留下评论.x