Python实现对word文档添加密码去除密码

代码实现如下:

import win32com.client,os,time

def word_encryption(path, password):
    # 若加密保存.docx时,覆盖原文件,则无法成功添加密码。但是保存为另一个文件名,则可以添加密码。
    # 因此将A存为B,删A,再将B改为A。
    dirname, tempname = os.path.split(path)
    path_temp = os.path.join(dirname, tempname)
    while os.path.exists(path_temp):
        tempname = f'{len(tempname)}' + tempname
        path_temp = os.path.join(dirname, tempname)
    def encryption(fp, pt, pw):
        word_app = win32com.client.Dispatch('Word.Application')
        word_app.Visible = 0
        word_app.DisplayAlerts = 0
        doc = word_app.Documents.Open(fp, False, False, False, '')
        doc.SaveAs2(pt, None, False, pw)
        doc.Close()
        word_app.Quit()

    encryption(path, path_temp, password)
    os.remove(path)  # 删除原文件
    os.rename(path_temp, path)  # 改临时文件名称为原文件名称
    time.sleep(0.5)  # 不要删除,不要删除
def word_decryption(path, password):
    # 若加密保存.docx时,覆盖原文件,则无法成功添加密码。但是保存为另一个文件名,则可以添加密码。
    # 因此将A存为B,删A,再将B改为A。
    dirname, tempname = os.path.split(path)
    path_temp = os.path.join(dirname, tempname)
    while os.path.exists(path_temp):
        tempname = f'{len(tempname)}' + tempname
        path_temp = os.path.join(dirname, tempname)
    def decryption(fp, pt, pw):
        word_app = win32com.client.Dispatch('Word.Application')
        word_app.Visible = 0
        word_app.DisplayAlerts = 0
        doc = word_app.Documents.Open(fp, False, False, False, key)
        doc.SaveAs2(pt, None, False, pw)
        doc.Close()
        word_app.Quit()

    decryption(path, path_temp, password)
    os.remove(path)  # 删除原文件
    os.rename(path_temp, path)  # 改临时文件名称为原文件名称
    time.sleep(0.5)  # 不用删除

def elistdir(path):
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path) and file_path==path:#排除子路径
            elistdir(file_path)
            #print(file_path)
        elif os.path.splitext(file_path)[1]=='.docx':
            #list_name.append(file_path)
            
            if file_path != '':
                print(file_path)
                try:
                    word_encryption(file_path, key)
                except:
                    pass

def dlistdir(path):
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path) and file_path==path:#排除子路径
            dlistdir(file_path)
            #print(file_path)
        elif os.path.splitext(file_path)[1]=='.docx':
            #list_name.append(file_path)
            
            if file_path != '':
                print(file_path)
                try:
                    word_decryption(file_path, '')
                except:
                    pass

if __name__ == '__main__':
    key='12345'                                 #加密解密密匙
    filedir=r"C:\Users\Administrator\Desktop"#  指定路径不包含子路径
    elistdir(filedir) #遍历word
    print('encrytion sucess\n Waiting...')
    time.sleep(2)#设置时间随意操作
    dlistdir(filedir) #遍历word
    print('decrytion Done')

实现:

 

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
先进行文本预处理,包括去除停用词、特殊符号、数字等,对文本进行分词并进行词性标注和去除标点符号。然后利用TF-IDF算法计算文本相似度,通过设置阈值来判断两篇文档是否相似。具体实现代码如下: ```python import jieba import jieba.analyse import jieba.posseg as pseg import math def preprocess(text): """ 对文本进行预处理,包括去除停用词、特殊符号、数字等 """ # 去除特殊符号和数字 text = re.sub('[^\\u4e00-\\u9fa5a-zA-Z]', '', text) # 分词并去除停用词 words = [w.word for w in pseg.cut(text) if w.flag[0] not in ['x', 'u', 'w'] and w.word not in stopwords] return words def get_tf(words): """ 计算单词的词频 """ tf = {} for word in words: if word not in tf: tf[word] = 0 tf[word] += 1 return tf def get_idf(words, idf_dict): """ 计算单词的逆文档频率 """ idf = {} for word in words: if word not in idf: idf[word] = math.log(len(idf_dict) / (idf_dict.get(word, 0) + 1)) return idf def get_tfidf(tf, idf): """ 计算单词的TF-IDF值 """ tfidf = {} for word in tf: tfidf[word] = tf[word] * idf[word] return tfidf def get_similarity(tfidf1, tfidf2): """ 计算两个文档的相似度 """ numerator = 0 denominator1 = 0 denominator2 = 0 for word in tfidf1: numerator += tfidf1[word] * tfidf2.get(word, 0) denominator1 += tfidf1[word] ** 2 for word in tfidf2: denominator2 += tfidf2[word] ** 2 denominator = math.sqrt(denominator1) * math.sqrt(denominator2) if denominator == 0: return 0 else: return numerator / denominator def is_duplicate(text1, text2, threshold=0.8): """ 判断两篇文档是否相似 """ words1 = preprocess(text1) words2 = preprocess(text2) tf1 = get_tf(words1) tf2 = get_tf(words2) idf_dict = dict.fromkeys(set(words1 + words2), 0) idf_dict.update(get_idf(words1, idf_dict)) idf_dict.update(get_idf(words2, idf_dict)) tfidf1 = get_tfidf(tf1, idf_dict) tfidf2 = get_tfidf(tf2, idf_dict) similarity = get_similarity(tfidf1, tfidf2) if similarity >= threshold: return True else: return False ``` 调用该函数可以判断两篇文档是否相似,例如: ```python text1 = "这是一篇测试文档,用于测试相似度计算。" text2 = "这篇文档是用来测试相似度计算的。" if is_duplicate(text1, text2): print("两篇文档相似") else: print("两篇文档不相似") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值