-1

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.

jeepers mcface
  • 371
  • 1
  • 5
  • 15
  • Possible duplicate of [PermissionError \[errno 13\] when running openpyxl python script in Komodo](https://stackoverflow.com/questions/31147460/permissionerror-errno-13-when-running-openpyxl-python-script-in-komodo) – Charlie Clark Jan 31 '19 at 18:04
  • @CharlieClark Correct. Sorry--I thought I tried what was specified in that thread. – jeepers mcface Jan 31 '19 at 18:12

1 Answers1

1

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.

anand_v.singh
  • 2,768
  • 1
  • 16
  • 35
  • Wow. I could have sworn I tried that... But yeah, it works. The only problem is now that when I go back to open it, it says "Excel cannot open the file 'Practice.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file." But when I look at the properties of the original file, it is indeed called "Practice" and the type of file is Microsoft Excel Macro-Enabled Worksheet (.xlsm). Where am I going wrong here. – jeepers mcface Jan 31 '19 at 18:09
  • https://bitbucket.org/openpyxl/openpyxl/issues/294/reading-and-writing-an-xlsm-file-results This should help you through that. @jeepers mcface – anand_v.singh Jan 31 '19 at 18:26