因此,我试图打开多个excel工作簿并对其进行更改,但是它们受到保护,并且我不知道如何实现xlrd使其正常工作。一切都适用于不受保护的工作簿,但是当我向它们添加密码时,我得到了错误XLRDError: Can't find workbook in OLE2 compound document
链接到data = pd.read_excel(f,header=2)
部分。
这是我的代码:
import os
import win32com.client
import glob
import pandas as pd
from xlrd import *
path = r'C:\...'
files = os.listdir(path)
dfs = []
for f in glob.glob(path + "/*.xlsx"):
**xlApp = win32com.client.Dispatch("Excel.Application")
xlwb = xlApp.Workbooks.Open(f, False, True, None, 'fernanda')**
data = pd.read_excel(f,header=2)
dfs.append(data)
"""Columns we're going to to manipulate"""
date = "DATE OF CHANGE"
id = "ID"
df = pd.concat(dfs)
df[date] = pd.to_datetime(df[date], errors='coerce')
master = df.groupby(id).min()
for i in dfs:
i[date] = i.join(master,rsuffix='_adj',on=id)[[date+"_adj"]]
r = zip(dfs,glob.glob(path + "/*.xlsx"))
r_list = list(r)
for i in r_list:
writer = pd.ExcelWriter(i[1])
i[0].to_excel(writer,'sheet1', startrow=2)
writer.save()
有人可以帮忙吗?谢谢
答案 0 :(得分:0)
您可以尝试一下,只需为您的循环进行修改
xl = Dispatch("Excel.Application")
#open password protected workbook, link on this command at the bottom
wb = xl.Workbooks.Open(filename, None, None, None, "your password")
ws = wb.Worksheets("Sheet1")
data = ws.Range("A1:D4")
#now data.value will return a tuple of tuples that you can put in a dataframe
values = data.value
#assuming the first row is the header
df = pd.DataFrame(values[1:],columns=values[0])
https://docs.microsoft.com/en-us/office/vba/api/Excel.Workbooks.Open