I have a word document that I will need to convert to an image. Basically opening the doc and taking a screencap of its content. Is there any library to do this?
Asked
Active
Viewed 2,205 times
1
-
Based on your approach, look into spawning a subprocess for 'opening the doc'. The 'taking a screencap' portion may be OS dependent (pyautogui vs https://stackoverflow.com/questions/69645/take-a-screenshot-via-a-python-script-on-linux) – Abel Jul 31 '20 at 19:00
1 Answers
2
This code will do most of the work. It opens Word, loads the document, takes a screenshot, then closes Word. It maximizes Word and the screenshot is the entire screen. You will probably need to do additional image processing to get the region you want.
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()

Mike67
- 11,175
- 2
- 7
- 15