deepl翻译的PDF文档保护密码解除

1、首先将后缀名(.docx)修改为压缩包格式(.zip)。

2、修改解密word加密.py里zip的位置,和新生成的zip的位置和名称

bash 复制代码
import zipfile
import xml.etree.ElementTree as ET
import os
import shutil

# 定义文件路径
zip_file_path = r'C:\Users\Administrator\Desktop\新建文件夹 (3)\小说 en.zip'
new_zip_file_path = r'C:\Users\Administrator\Desktop\新建文件夹 (3)\小说 en_modified.zip'
xml_file_path_in_zip = 'word/settings.xml'

# 创建临时目录
temp_dir = 'temp_zip'
os.makedirs(temp_dir, exist_ok=True)

# 打开 ZIP 文件
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
    # 解压缩所有文件到临时目录
    zip_ref.extractall(temp_dir)

# 构建完整的 XML 文件路径
xml_file_full_path = os.path.join(temp_dir, xml_file_path_in_zip)

# 解析 XML 文件,并指定命名空间
tree = ET.parse(xml_file_full_path)
root = tree.getroot()

# 定义命名空间
namespace = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

# 查找并修改 w:documentProtection 元素的 w:enforcement 属性
document_protection = root.find('.//w:documentProtection', namespace)
if document_protection is not None:
    if '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}enforcement' in document_protection.attrib:
        document_protection.attrib['{http://schemas.openxmlformats.org/wordprocessingml/2006/main}enforcement'] = 'false'

# 将修改后的 XML 文件保存回临时目录
tree.write(xml_file_full_path, encoding='utf-8', xml_declaration=True, default_namespace='http://schemas.openxmlformats.org/wordprocessingml/2006/main')

# 创建新的 ZIP 文件
with zipfile.ZipFile(new_zip_file_path, 'w', zipfile.ZIP_DEFLATED) as new_zip_ref:
    for foldername, subfolders, filenames in os.walk(temp_dir):
        for filename in filenames:
            file_path = os.path.join(foldername, filename)
            arcname = os.path.relpath(file_path, temp_dir)
            new_zip_ref.write(file_path, arcname)

# 清理临时目录
shutil.rmtree(temp_dir)

print(f'修改后的 ZIP 文件已保存到: {new_zip_file_path}')

3、运行解密word加密.py

4、将新生成的zip后缀改回.docx

5、打开.docx看是否已经解除密码保护了

相关推荐
马武寨山的猴子12 小时前
【MinerU】:一款将PDF转化为机器可读格式的工具——RAG加强(Docker版本)
人工智能·docker·容器·pdf·rag
萧鼎13 小时前
PDFMathTranslate:让数学公式在PDF翻译中不再痛苦
python·pdf
会功夫的李白13 小时前
PDF嵌入隐藏的文字
java·pdf·itext
陌殇殇1 天前
Java使用IText7动态生成带审批文本框的PDF文档
java·pdf
Ven%2 天前
VsCode如何使用默认程序打开word Excel pdf等文件
ide·vscode·pdf·编辑器
软件工程小施同学2 天前
计算机学报 2024年 区块链论文 录用汇总 附pdf下载
pdf·区块链
Sherlock Ma2 天前
PDFMathTranslate:基于LLM的PDF文档翻译及双语对照的工具【使用教程】
人工智能·pytorch·语言模型·pdf·大模型·机器翻译·deepseek
kadog2 天前
PubMed PDF下载 cloudpmc-viewer-pow逆向
前端·javascript·人工智能·爬虫·pdf
小墨宝3 天前
js 生成pdf 并上传文件
前端·javascript·pdf
热水养鲨鱼3 天前
Java实现HTML转PDF(deepSeekAi->html->pdf)
人工智能·pdf·html