The variable names were translated from portuguese to facilitate understanding, so, if there is a typo here, it is probably not present in the real code
with pd.ExcelWriter(fileName, engine='openpyxl') as writer1:
try:
writer1.book=openpyxl.load_workbook(fileName)
writer1.sheets = dict((ws.title, ws) for ws in writer1.book.worksheets)
except Exception as e:
print(e)
print("exception occured")
writer1.book = openpyxl.Workbook()
for i in range(len(regions)):
region=list(regions)[i]
if(not region in numberOfRowsByRegion):
numberOfRowsByRegion[region]=0
finalTable=pd.DataFrame(regions[region],columns=[]) #Not relevant column names here
finalTable.to_excel(writer1, sheet_name = region, startrow=numberOfRowsByRegion[region]+((not firstTimeWriting)*1), index = False, header=firstTimeWriting)
numberOfRowsByRegion[region]+=len(regions[region])
This code was running perfectly in my own PC, but when i try to run it in my work's PC, i get an error every time this code runs(the "with" is inside a "for"), in my PC, i got a "No such file or directory" error in(and only in) the first iteration, but in my work's PC, i get a "File is not a zip file" in every iteration, which means that the writting occurs with a new empty Workbook, meaning that the previous rows are not kept. I already tried to create the excel file before execution with Excel, nothing changes
The file extension is ".xlsx"
EDIT: full traceback of the error when i add sys.exit() in the "except" part:
Traceback (most recent call last):
File "c:UsersmyUserDocumentscodespythonpdfScrappermain.py", line 116, in <module>
writer1.book=openpyxl.load_workbook(nomeArquivo)
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxl
eaderexcel.py", line 313, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxl
eaderexcel.py", line 124, in __init__
self.archive = _validate_archive(fn)
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxl
eaderexcel.py", line 96, in _validate_archive
archive = ZipFile(filename, 'r')
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libzipfile.py", line 1269, in __init__
self._RealGetContents()
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libzipfile.py", line 1336, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:UsersmyUserDocumentscodespythonpdfScrappermain.py", line 122, in <module>
sys.exit()
SystemExit
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:UsersmyUserDocumentscodespythonpdfScrappermain.py", line 131, in <module>
numberOfRowsByRegion[region]+=len(regions[region])
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagespandasioexcel\_base.py", line 883, in __exit__
self.close()
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagespandasioexcel\_base.py", line 887, in close
content = self.save()
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagespandasioexcel\_openpyxl.py", line 51, in save
self.book.save(self.handles.handle)
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxlworkbookworkbook.py", line 392, in save
save_workbook(self, filename)
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxlwriterexcel.py", line 293, in save_workbook
writer.save()
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxlwriterexcel.py", line 275, in save
self.write_data()
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxlwriterexcel.py", line 89, in write_data
archive.writestr(ARC_WORKBOOK, writer.write())
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxlworkbook\_writer.py", line 148, in write
self.write_views()
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxlworkbook\_writer.py", line 135, in write_views
active = get_active_sheet(self.wb)
File "C:UsersmyUserAppDataLocalProgramsPythonPython38libsite-packagesopenpyxlworkbook\_writer.py", line 33, in get_active_sheet
raise IndexError("At least one sheet must be visible")
IndexError: At least one sheet must be visible