python实现word图片文字分离

需要先装pywin32-218.win-amd64-py3.3
#coding:utf-8
from win32com import client as wc   
import os   
import glob
word = wc.Dispatch('Word.Application')   
  
def wordsToHtml(dir):   
            #得到要处理的word后缀为doc文件列表
            filelist1 = glob.glob(dir+'\*.doc')
            #print (filelist1)
            for wardfullName in filelist1:
                doc = word.Documents.Open(wardfullName) 
                htmlfullName = wardfullName[:-3]+'html'
                txtfullName = wardfullName[:-3]+'txt'

                print('正在处理图片----------'+htmlfullName)
                print('正在处理文字----------'+txtfullName)
            
                doc.SaveAs(htmlfullName, 10) 
                doc.SaveAs(txtfullName,5)  

                os.remove(htmlfullName)
                print('正在删除html文件----------'+htmlfullName)
                doc.Close() 
            #得到要处理的word后缀为docx文件列表
            filelist2 = glob.glob(dir+'\*.docx')
            #print (filelist2)
            for wardfullName in filelist2:
                doc = word.Documents.Open(wardfullName) 
                htmlfullName = wardfullName[:-4]+'html'
                txtfullName = wardfullName[:-4]+'txt'

                print('正在处理图片----------'+htmlfullName)
                print('正在处理文字----------'+txtfullName)
            
                doc.SaveAs(htmlfullName, 10) 
                doc.SaveAs(txtfullName,5)  

                os.remove(htmlfullName)
                print('正在删除html文件----------'+htmlfullName)
                doc.Close()       
            word.Quit()   
     

if __name__ == '__main__':
    ddir = r'F:\python'
    wordsToHtml(ddir)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值