尝试构建 Excel 电子表格密码删除器
Trying to build a Excel Spreadsheet Password remover
首先:
我根本不是程序员。我是一名系统管理员,主要可以阅读不同类型的脚本或编程语言,并且能够稍微理解它们。我构建此工具的原因:我厌倦了每周为忘记密码的用户做这种类型的工作 Excel 列表。
我已经完成了大部分工具。在 Windows 系统上使用 Python3 作为语言 +。
我目前唯一苦苦挣扎的是,我修改了我的精彩修改 *.xml 文件(当通过 7-Zip 手动将其推回 xl\worksheets\ 文件夹时,它按预期工作)但是不要' 似乎找到了一种通过 python 将文件推回 Excel 容器的方法。
这里有人知道我该怎么做吗?
我已经尝试通过 zipfile 推回文件,但这似乎不起作用,因为 excel 文件本身的结构。
而且 7Zip 的命令行对我没有帮助,或者我现在只是愚蠢。
感谢任何帮助^^
当前代码。快 n 脏:
import os
import sys
import time
import errno
import shutil
import zipfile
import re
from os.path import basename
def createFolder(foldername):
try:
#Create Directory
os.mkdir(foldername)
print("Directory " , foldername , "created! ")
except FileExistsError:
print("Directory" , foldername , "already exists!")
def repppydirs():
# Get current workdir
global repppyimport, repppytemp, repppyexport
workindir = os.getcwd()
repppyimport = workindir + '\import'
repppytemp = workindir + '\temp'
repppyexport = workindir + '\export'
createFolder(repppyimport)
createFolder(repppytemp)
createFolder(repppyexport)
# Replaces the Password String inside the XML Sheets
def findPasswordLine(inputSource,tempdir,filename):
input = open(inputSource)
output = open(tempdir + '\' + filename,'w')
for line in input:
# REgex Helper ;) https://pythex.org/
output.write(re.sub('<sheetProtection.*?.>', '', line))
input.close()
output.close()
def writeback2excel(zip_file,folder,file):
z = zipfile.ZipInfo()
z.filename = "xl\worksheets"
filedata = open()
def main(rimport,rtemp,rexport):
listOfFile = os.listdir(rimport)
#FileList = list()
for file in listOfFile:
print(file)
# Copy file to work with
src_dir = rimport + '\' + file
dst_temp_dir = rtemp + '\temp_' + file
dst_exp_dir = rexport + '\reppy_' + file
shutil.copy(src_dir,dst_temp_dir)
shutil.copy(src_dir,dst_exp_dir)
with zipfile.ZipFile(dst_temp_dir,'r') as zip_ref:
ziptmp = rtemp + '\zip'
createFolder(ziptmp)
zip_ref.extractall(ziptmp)
modxmlpath = ziptmp + '\xl\worksheets'
modxmltree = os.listdir(modxmlpath)
tempxl = rtemp + '\xl'
createFolder(tempxl)
srtemp = tempxl + '\worksheets'
createFolder(srtemp)
for mxml in modxmltree:
if(mxml.endswith('.xml')):
print(mxml)
findPasswordLine(modxmlpath + '\' + mxml,srtemp,mxml)
writeback2excel(dst_exp_dir,srtemp,mxml)
return
## Call to action
repppydirs()
main(repppyimport,repppytemp,repppyexport)
在对 7zip 命令行进行修改之后,我发现它比想象的要容易得多:)
对于任何其他想要用 sheetX.xml 文件更新 Excel 文件的人,您需要做两件事。
首先需要将sheetX.xml个文件导出到'\xl\worksheets\'
的目录结构中
在此之后您可以下载 7zip cmd 行版本并将所有内容一起放入您的 python 项目并使用如下所示的更新功能:
.za.exe u '..\export\yourexcelfile.xlsm' '..\folderAbovexl\*'
这将完成将所有内容写回 excel 文件的技巧。
如果你只想复制粘贴:
def writeback2excel(zip_file,workindir):
# 7Zip Locations
z7location = workindir + '\7z\7za.exe'
# Export with 7z
os.system(f'{z7location} u "{zip_file}" "{workindir}\temp\export\*" ')
然后在我的问题 Post 中做一些调整,你就有了一个基于 python 的 excel 电子表格密码删除器。
玩得开心 :)
编辑:Post等 github https://github.com/pabumake/reppy
首先:
我根本不是程序员。我是一名系统管理员,主要可以阅读不同类型的脚本或编程语言,并且能够稍微理解它们。我构建此工具的原因:我厌倦了每周为忘记密码的用户做这种类型的工作 Excel 列表。
我已经完成了大部分工具。在 Windows 系统上使用 Python3 作为语言 +。 我目前唯一苦苦挣扎的是,我修改了我的精彩修改 *.xml 文件(当通过 7-Zip 手动将其推回 xl\worksheets\ 文件夹时,它按预期工作)但是不要' 似乎找到了一种通过 python 将文件推回 Excel 容器的方法。
这里有人知道我该怎么做吗?
我已经尝试通过 zipfile 推回文件,但这似乎不起作用,因为 excel 文件本身的结构。
而且 7Zip 的命令行对我没有帮助,或者我现在只是愚蠢。
感谢任何帮助^^
当前代码。快 n 脏:
import os
import sys
import time
import errno
import shutil
import zipfile
import re
from os.path import basename
def createFolder(foldername):
try:
#Create Directory
os.mkdir(foldername)
print("Directory " , foldername , "created! ")
except FileExistsError:
print("Directory" , foldername , "already exists!")
def repppydirs():
# Get current workdir
global repppyimport, repppytemp, repppyexport
workindir = os.getcwd()
repppyimport = workindir + '\import'
repppytemp = workindir + '\temp'
repppyexport = workindir + '\export'
createFolder(repppyimport)
createFolder(repppytemp)
createFolder(repppyexport)
# Replaces the Password String inside the XML Sheets
def findPasswordLine(inputSource,tempdir,filename):
input = open(inputSource)
output = open(tempdir + '\' + filename,'w')
for line in input:
# REgex Helper ;) https://pythex.org/
output.write(re.sub('<sheetProtection.*?.>', '', line))
input.close()
output.close()
def writeback2excel(zip_file,folder,file):
z = zipfile.ZipInfo()
z.filename = "xl\worksheets"
filedata = open()
def main(rimport,rtemp,rexport):
listOfFile = os.listdir(rimport)
#FileList = list()
for file in listOfFile:
print(file)
# Copy file to work with
src_dir = rimport + '\' + file
dst_temp_dir = rtemp + '\temp_' + file
dst_exp_dir = rexport + '\reppy_' + file
shutil.copy(src_dir,dst_temp_dir)
shutil.copy(src_dir,dst_exp_dir)
with zipfile.ZipFile(dst_temp_dir,'r') as zip_ref:
ziptmp = rtemp + '\zip'
createFolder(ziptmp)
zip_ref.extractall(ziptmp)
modxmlpath = ziptmp + '\xl\worksheets'
modxmltree = os.listdir(modxmlpath)
tempxl = rtemp + '\xl'
createFolder(tempxl)
srtemp = tempxl + '\worksheets'
createFolder(srtemp)
for mxml in modxmltree:
if(mxml.endswith('.xml')):
print(mxml)
findPasswordLine(modxmlpath + '\' + mxml,srtemp,mxml)
writeback2excel(dst_exp_dir,srtemp,mxml)
return
## Call to action
repppydirs()
main(repppyimport,repppytemp,repppyexport)
在对 7zip 命令行进行修改之后,我发现它比想象的要容易得多:)
对于任何其他想要用 sheetX.xml 文件更新 Excel 文件的人,您需要做两件事。
首先需要将sheetX.xml个文件导出到'\xl\worksheets\'
在此之后您可以下载 7zip cmd 行版本并将所有内容一起放入您的 python 项目并使用如下所示的更新功能:
.za.exe u '..\export\yourexcelfile.xlsm' '..\folderAbovexl\*'
这将完成将所有内容写回 excel 文件的技巧。
如果你只想复制粘贴:
def writeback2excel(zip_file,workindir):
# 7Zip Locations
z7location = workindir + '\7z\7za.exe'
# Export with 7z
os.system(f'{z7location} u "{zip_file}" "{workindir}\temp\export\*" ')
然后在我的问题 Post 中做一些调整,你就有了一个基于 python 的 excel 电子表格密码删除器。
玩得开心 :)
编辑:Post等 github https://github.com/pabumake/reppy