python file processing -- document restriction edit clear password widget

Keywords: xml Python Excel

python file processing -- document restriction edit clear password widget

Usage scenarios

When using ofice document editing, set the password of document restriction editing, sometimes forget the password, causing trouble. This tool is developed by python to restrict editing and clearing passwords. You can set restrictions on editing passwords for word documents such as docx and doc, excel documents of xlsx and PPT documents of pptx to quickly clear passwords.

Introduction to the use of tools

1. Open the tool

2. Click to select file

You can choose docx, doc, xlsx, pptx and other formats.
word documents limit editing status.
excel document sheet protection, workbook protection, limit editing status.
ppt documents limit editing status.
3. Click to remove the password

If the conditions are met, the password is cleared successfully, and a reminder will pop up.
If the conditions are not met, a reminder that cannot be cleared will pop up.

Key code

1. File rename

os.rename(oldname, newname)  # rename
os.remove(myfile)  # Delete specified file

2. File path

import os
mydir = os.getcwd()  # The system obtains the current path in the form of backslash\
mydir = mydir.replace("\\", "/")  # Convert path anti diagonal bar to positive diagonal bar

3. Open the zip package and read

import zipfile
# Determine whether it is a zip package
checked = zipfile.is_zipfile(myfile)  # Returns True or False
zin = zipfile.ZipFile(myfile, 'r')  # Open package read-only
zout = zipfile.ZipFile(myfile_new, 'w')  # Open compressed package in write mode
for item in zin.infolist():  # Traversing files in a compressed package
	buffer =  # buffer is to read the file content, and item.filename is the file name
	zout.writestr(item, buffer)  # Write the contents of the file to the same object of the new file for compressed package replication
f =, 'r')  # Open the file in the package
text =  # Read file contents
f.close()  # Close file
zout.close()  # Turn off compression

4. Decompress the zip package

zin.extractall(path=showFile)  # Extract all files to the specified path without writing the parameter to the current path
zin.extract(bookfile, path=mypath)  # Extract the specified file to the specified path without writing the parameter path as the current path
  1. regular expression
import re
protect ='<sheetProtection.*?/>', filetext)  # Find protection configuration
if protect != None:
	protecttext =  # Get found content
  1. XML file editing
import xml.etree.ElementTree as ET
dom = ET.parse("abc.xml")  # Open xml file
root = dom.getroot()  # Get root tag
for child in root:  # Traverse to get the first level child tag
element = root.find(childtag)  # Find the specified label
root.remove(element)  # delete a tap
dom.write(mpath2)  # Rewrite xml file
  1. Delete non empty folder program
import shutil
filelists = os.listdir(dirpath)  # Get the list of all files in the directory
    for mydir in filelists:  # Traverse file list
        filepath = os.path.join(dirpath, mydir)  # Splicing file names
        if os.path.isfile(filepath):  # Determine whether the file is a file
            os.remove(filepath)  # If it is a file, delete it directly
        elif os.path.isdir(filepath):  # Determine whether the file is a folder
            shutil.rmtree(filepath, True)  # If it is a folder, delete the folder and all files in the folder
    shutil.rmtree(dirpath, True)  # Last delete root folder

This procedure goes to
8. Text format conversion

newfiletext = newfiletext.encode('utf-8')  # str to byte
newfiletext = newfiletext.decode().strip()  # Convert byte to str

Tool download address

The download address of the tool is:

Online ico icon conversion address:

Published 12 original articles, won praise 2, visited 2350
Private letter follow

Posted by rsasalm on Fri, 06 Mar 2020 23:05:23 -0800