记录excel(有密码)打不开,用python 还原。

记录excel(有密码)打不开,用python 还原。

from win32com.client import DispatchEx


def read_excel_password(filename, password):
    excel = DispatchEx("Excel.Application")  # 启动excel
    excel.Visible = False  # 去掉可视化
   
    demo = excel.Workbooks.Open(filename, UpdateLinks=False, ReadOnly=False, Format=None, Password=password)  # 打开文件并将密码传入

    #xlSheet_1 = demo.Worksheets(1)  # 打开第一个sheet
    demo.SaveAs("d:\\aaa.xls")  # 另存为
   
    #print(xlSheet_1.Cells(5, 4).Value)  # 打印第一个sheet的第一行一列数据
   
    demo.Close(True)  # 关闭文件
   
if __name__ == '__main__':
    filename = "E:\\xxx.xls"
    password = "xxx"
    read_excel_password(filename, password)