试图建立一个Excel电子表格密码清除器。

问题描述 投票:0回答:1

首先,我不是一个程序员。

我根本就不是一个程序员 我是一个系统管理员,主要是能读懂不同类型的脚本或编程语言&并对它们有一定的了解。我之所以建立这个工具。我厌倦了每周为用户做这种工作,因为他们忘记了自己Excel列表的密码。

我完成了工具的大部分。在Windows系统上使用Python3作为语言+.目前唯一苦恼的是,我已经修改了我的精彩的*.xml文件(当通过7-Zip手动推送回XL/worksheets文件夹时,它的工作原理和预期的一样),但似乎没有找到一种方法来通过python将文件推送回Excel容器中。

这里有谁知道如何做到这一点?

我已经尝试过通过zipfile来推送文件,但由于excel文件本身的结构,这似乎不起作用。

而且7Zip的命令行对我来说也没有任何帮助,或者说我现在太笨了。

任何帮助感谢^^"

当前代码。Quick n Dirty:

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)
python-3.x excel 7zip
1个回答
0
投票

在使用了7zip命令行之后,我发现它比想象中的要简单得多:)

对于其他人想要更新Excel文件和它的sheetX.xml文件,你需要做两件事。

首先,你需要把sheetX.xml文件导出到一个directoy结构的文件中。'\xl\worksheets\'

在这之后,你可以下载7zip cmd line版本,然后把所有的东西都放到你的python项目中,并使用更新功能,如下面所示。

.\7za.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\\*" ')

并在我的问题帖中做一些调整,你就有了一个基于python的excel电子表格密码清除器。

有一个好的:)

EDIT: Postet on github https:/github.compabumakereppy。

© www.soinside.com 2019 - 2024. All rights reserved.