-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunprotect_sheet.py
More file actions
65 lines (47 loc) · 2.05 KB
/
Copy pathunprotect_sheet.py
File metadata and controls
65 lines (47 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import re
import shutil
import zipfile
import argparse
def remove_sheet_protection_tags(xml_folder: str) -> None:
"""Remove <sheetProtection .../> tags from all .xml files in the given folder."""
pattern = r'<sheetProtection\b[^<>]*?(?:/>|></sheetProtection>)'
for filename in os.listdir(xml_folder):
if filename.endswith('.xml'):
file_path = os.path.join(xml_folder, filename)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
cleaned_content = re.sub(pattern, '', content)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(cleaned_content)
def main():
parser = argparse.ArgumentParser(description='Remove sheet protection from .xlsx sheets.')
parser.add_argument('-i', '--input', required=True, help='Input .xlsx file')
args = parser.parse_args()
input_file = os.path.basename(args.input)
if not input_file.lower().endswith('.xlsx'):
raise argparse.ArgumentTypeError(f'The file "{input_file}" is not a .xlsx file.')
# Output and temp names
output_file = f'Unprotect{input_file}'
temp_xlsx = '_temp.xlsx'
temp_zip = '_temp.zip'
temp_dir = '_temp'
# Step 1: Copy and rename input file to .zip
shutil.copy2(input_file, temp_xlsx)
os.rename(temp_xlsx, temp_zip)
# Step 2: Extract .zip content to temporary directory
with zipfile.ZipFile(temp_zip, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
# Step 3: Remove <sheetProtection> tags from sheet XML files
worksheets_path = os.path.join(temp_dir, 'xl', 'worksheets')
remove_sheet_protection_tags(worksheets_path)
# Step 4: Rezip the modified content
shutil.make_archive('_temp', 'zip', temp_dir)
os.rename(temp_zip, output_file)
# Step 5: Clean up
shutil.rmtree(temp_dir)
if os.path.exists(temp_zip):
os.remove(temp_zip)
print(f'File "{output_file}" created successfully!!')
if __name__ == '__main__':
main()