判断数据库和磁盘中文件是否一致

from DB import MSSQLDB
from openpyxl import load_workbook
import fnmatch, os, sys, win32com.client
import re

txtpath=r'C:\360高速下载' #绝对路径
count=0 #记录处理的数据行数
txtcount=0#记录doc或docx文件复制为txt文件的文件名
wordapp = win32com.client.Dispatch("Word.Application")  
db=MSSQLDB()
resList=db.ExecQuery(" select name  from lq_testdoc  ")
try: 
    for r in resList:
        filename=r[0]
        if os.path.exists(filename):
            if  fnmatch.fnmatch(filename, '*.txt'):
##                txtdeal = os.path.abspath(os.path.join(path,filename))
                picnumstr='0'
                f=open(filename,'r')
                strlines=f.readlines()
                string="".join(list(strlines))
                f.close()
                string=string.replace("'"," ")
                db.ExecNonQuery("insert into lq_data(filename,filecontent,flag) values ('"+filename+"','"+string+"','"+picnumstr+"')" );
                count=count+1
                print('Succeed--第'+str(count)+'行数据')
            elif  fnmatch.fnmatch(filename, '*.docx') or fnmatch.fnmatch(filename, '*.doc'):
##                    doc = os.path.abspath(os.path.join(path,filename))
                    docc = wordapp.Documents.Open(filename)
                    picnum=docc.InlineShapes.Count
                    docc.Close()
                    if picnum!=0:
                        picnum=1
##                    if fnmatch.fnmatch(filename, '*.doc'):
##                        docastext = filename[:-3] + 'txt' 
##                    else:
##                        docastext = filename[:-4] + 'txt'
                    txtcount=txtcount+1
                    txtname=str(txtcount)+'.txt'
                    docastext=os.path.abspath(os.path.join(txtpath,txtname))
                    wordapp.Documents.Open(filename)
                    wordapp.ActiveDocument.SaveAs(docastext,FileFormat=win32com.client.constants.wdFormatText)  
                    wordapp.ActiveDocument.Close()
                    picnumstr=str(picnum)
                    f=open(docastext,'r')
                    strlines=f.readlines()
                    string="".join(list(strlines))
                    f.close()
                    string=string.replace("'"," ")
                    db.ExecNonQuery("insert into lq_data(filename,filecontent,flag) values ('"+filename+"','"+string+"','"+picnumstr+"')" );
##                    os.remove(docastext)
                    count=count+1
                    print('Succeed--第'+str(count)+'行数据')
            else:
                db.ExecNonQuery("insert into lq_data(filename,filecontent) values ("+
                                "'"+filename+"',"+
                                "'该文件不是{docx,doc,txt}格式的文件')");
                count=count+1
                print('Succeed--第'+str(count)+'行数据')
                continue   
        else:
            db.ExecNonQuery("insert into lq_data(filename,filecontent) values ("+
                            "'"+filename+"',"+
                            "'给出的绝对路径下该文件不存在')");
            count=count+1
            print('Succeed--第'+str(count)+'行数据')
            continue     
finally:
    wordapp.Quit()
    print ('end') 
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值