Extrahieren Sie alle Bilder in einer DOCX -Datei mit PythonPython

Python-Programme
Anonymous
 Extrahieren Sie alle Bilder in einer DOCX -Datei mit Python

Post by Anonymous »

Ich habe eine DOCX-Datei, die 6-7 Bilder enthält. Ich muss die Extraktion von Bildern aus dieser DOC -Datei automatisieren. Gibt es eine Win32com ms Word -API für dasselbe? Oder eine Bibliothek, die alle Bilder genau darin extrahieren kann?

Code: Select all

from pathlib import Path
from win32com.client import Dispatch

xls = Dispatch("Excel.Application")
doc = Dispatch("Word.Application")

def export_images(fp, prefix="img_", suffix="png"):
""" export all of images(inlineShapes) in the word file.
:param fp: path of word file.
:param prefix: prefix of exported images.
:param suffix: suffix of exported images.
"""

fp = Path(fp)
word = doc.Documents.Open(str(fp.resolve()))
sh = xls.Workbooks.Add()
for idx, s in enumerate(word.inlineShapes, 1):
s.Range.CopyAsPicture()
d = sh.ActiveSheet.ChartObjects().add(0, 0, s.width, s.height)
d.Chart.Paste()
d.Chart.Export(fp.parent / ("%s_%s.%s" % (prefix, idx, suffix))
sh.Close(False)
word.Close(False)
export_images(r"C:\Users\HPO2KOR\Desktop\Work\venv\us2017010202.docx")
Sie können die DOCX -Datei hier herunterladen>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post