近日要处理一批docx文件,面临doc转docx、删除密码等问题。在参阅了网上的资料以后发现pywin32的功能非常强大,但在网上的详细资料很少,查阅了很多资料,发现在stackoverflow上有一个解决方案。方案连接
源代码是ppt的密码检测,我修改成docx版本,先上代码:
#coding:utf-8
import win32gui
import win32con
import win32com.client
import threading
flag = False
def terminate():
global flag
while (1):
hwnd = win32gui.FindWindow(None, '密码')
if hwnd != 0:
print('有密码')
win32gui.PostMessage(hwnd,win32con.WM_CLOSE,0,0)
break
if flag == True:
break
filename = ‘你的doc文件位置’
t = threading.Thread(target=terminate)
t.start()
try:
doc = win32com.client.Dispatch("Word.Application")
doc.Documents.Open(filename, ReadOnly=True)
print(doc.value)
except: