Inspired by Automate the Boring Stuff with Python, I am doing just that! Everything seems to be going well until I try to save the workbooks I edit with openpyxl. I get an error only at that point in the code.
Here is the relevant part of the code (before this I use selenium to download the appropriate file):
#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")
I tried to just do paths instead of variables just to test it, and that didn't work either. But the paths must be accurate, or else I couldn't create a workbook object out of them--right? I also tried switching the order of my saving lines just to make sure that one of the workbooks wasn't the problem child. They both are.
You are trying to create a file and your OS is saying you that your program does not have the permission to write to that directory. You should specify the full path of the file you're trying to create. If it still fails, comment below and we will see.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments