python - 取消 Word 文档编辑限制

image

1. word格式转换

先将word转换为xml格式,路径需要绝对路径

from win32com import client
import os


def doc2docx(path):
    word = client.Dispatch("Word.Application")
    doc = word.Documents.Open(path)
    doc.SaveAs(f"{path[:-3]}docx", 12)
    doc.Close()
    word.Quit()


def docx2doc(path):
    word = client.Dispatch("Word.Application")
    doc = word.Documents.Open(path)
    doc.SaveAs(f"{path[:-4]}doc", 0)
    doc.Close()
    word.Quit()


def doc2xml(path):
    word = client.Dispatch("Word.Application")
    doc = word.Documents.Open(path)
    doc.SaveAs(f"{path[:-3]}xml", 11)
    doc.Close()
    word.Quit()


def docx2xml(path):
    word = client.Dispatch("Word.Application")
    doc = word.Documents.Open(path)
    doc.SaveAs(f"{path[:-4]}xml", 11)
    doc.Close()
    word.Quit()

# wdFormatDocument = 0
# wdFormatDocument97 = 0
# wdFormatDocumentDefault = 16
# wdFormatDOSText = 4
# wdFormatDOSTextLineBreaks = 5
# wdFormatEncodedText = 7
# wdFormatFilteredHTML = 10
# wdFormatFlatXML = 19
# wdFormatFlatXMLMacroEnabled = 20
# wdFormatFlatXMLTemplate = 21
# wdFormatFlatXMLTemplateMacroEnabled = 22
# wdFormatHTML = 8
# wdFormatPDF = 17
# wdFormatRTF = 6
# wdFormatTemplate = 1
# wdFormatTemplate97 = 1
# wdFormatText = 2
# wdFormatTextLineBreaks = 3
# wdFormatUnicodeText = 7
# wdFormatWebArchive = 9
# wdFormatXML = 11
# wdFormatXMLDocument = 12
# wdFormatXMLDocumentMacroEnabled = 13
# wdFormatXMLTemplate = 14
# wdFormatXMLTemplateMacroEnabled = 15

2. 实例

目录内的所有Word文档移除编辑限制

def xml2doc(path):
    word = client.Dispatch("Word.Application")
    doc = word.Documents.Open(path)
    doc.SaveAs(f"{path[:-3]}doc", 0)
    doc.Close()
    word.Quit()


def xml2docx(path):
    word = client.Dispatch("Word.Application")
    doc = word.Documents.Open(path)
    doc.SaveAs(f"{path[:-3]}docx", 12)
    doc.Close()
    word.Quit()


def changeXml(path):
    with open(path, "r", encoding="utf-8") as f:
        xml = f.read()
    # 将documentProtection更改为unDocumentProtection
    xml = xml.replace("documentProtection", "unDocumentProtection")
    with open(path, "w", encoding="utf-8") as f:
        f.write(xml)

rootPath = r"D:\test"
for filepath, dirnames, filenames in os.walk(rootPath):
    for filename in filenames:
        path = os.path.join(filepath, filename)
        print(path)
        if filename[-3:] == "doc":
            doc2xml(path)
            changeXml(f"{path[:-3]}xml")
            xml2doc(f"{path[:-3]}xml")
            os.remove(f"{path[:-3]}xml")
        if filename[-4:] == "docx":
            docx2xml(path)
            changeXml(f"{path[:-4]}xml")
            xml2docx(f"{path[:-4]}xml")
            os.remove(f"{path[:-4]}xml")
posted @ 2023-12-04 12:08  wstong  阅读(76)  评论(0编辑  收藏  举报