如何使用 python 将 word docx 转换为图像

How to convert a word docx to an image using python

我有一个 word 文档,需要将其转换为图像。基本上打开文档并截取其内容。有没有图书馆可以做到这一点?

例如:

此代码将完成大部分工作。它打开 Word,加载文档,截取屏幕截图,然后关闭 Word。它最大化 Word,屏幕截图是整个屏幕。您可能需要进行额外的图像处理才能获得所需的区域。

import win32com.client as win32
import pyautogui
import win32gui
import time

docfile = 'D:/test.docx'
shotfile = 'D:/shot.png'

def windowEnumerationHandler(hwnd, top_windows):
    top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
    
word = win32.gencache.EnsureDispatch('Word.Application')
word.Visible = True
word.WindowState = 1  # maximize

top_windows = []
win32gui.EnumWindows(windowEnumerationHandler, top_windows)

for i in top_windows:  # all open apps
   if "word" in i[1].lower(): # find word (assume only one)
       try:
          win32gui.ShowWindow(i[0],5)
          win32gui.SetForegroundWindow(i[0])  # bring to front
          break
       except:
          pass
    
doc = word.Documents.Add(docfile) # open file

time.sleep(2)  # wait for doc to load

myScreenshot = pyautogui.screenshot() # take screenshot
myScreenshot.save(shotfile) # save screenshot

# close doc and word app
doc.Close()
word.Application.Quit()