受Python自动完成无聊的启发,我正在这样做!直到我尝试保存用openpyxl编辑的工作簿之前,一切似乎都进展顺利。我只有在代码的那一点才出现错误。
这是代码的相关部分(在此之前,我使用硒下载相应的文件):
#Gets list of files from downloads
list_of_files = glob.glob('PATH/TO/DOWNLOADS/*')
#Finds most recently downloaded file
latest_file = max(list_of_files, key=os.path.getctime)
#Gets path to destination for sheet in downloaded spreadsheet
report = r'PATH/TO/DESTINATION'
#Appends r to string of latest_file path so that it can be read
pyxlOpen = r'%s' % latest_file
#Opens recently downloaded file as workbook object
snapshotWB = xl.load_workbook(pyxlOpen)
#Gets only sheet in the spreadsheet and renames it 'Snapshot'
snapSheet = snapshotWB.worksheets[0]
snapSheet.title = 'Snapshot'
#Opens destination workbook as workbook object
reportWB = xl.load_workbook("report")
#Gets rid of any old Snapshot sheet if it's in there
if 'Snapshot' in reportWB.sheetnames:
del reportWB['Snapshot']
#And add in the new one!
newSnapSheet = reportWB.create_sheet("Snapshot")
for row in snapSheet:
for cell in row:
newSnapSheet[cell.coordinate].value = cell.value
#Saves the workbooks!
snapshotWB.save(filename = 'latest_file')
reportWB.save(filename = "Practice.xlsm")
我只是尝试测试路径而不是变量,但这也不起作用。但是路径必须准确,否则我无法从中创建工作簿对象-对吗?我还尝试切换保存行的顺序,以确保其中一个工作簿不是问题孩子。他们都是。
答案 0 :(得分:0)
您正在尝试创建一个文件,并且您的操作系统在说您的程序无权写入该目录。您应指定要创建的文件的完整路径。如果仍然失败,请在下面评论,我们将看到。