I/O Error while saving Excel file - Python

2024/10/13 2:17:49

Im using python to open an existing excel file and do some formatting and save and close the file. My code is working good when the file size is small but when excel size is big (apprx. 40MB) I'm getting Serialization I/O error and Im sure it due to memory problem or due to my code. Kindly help.

System Config:

RAM - 8 GB 32 - bit operation Windows 7

Code:

import numpy as np
from openpyxl import load_workbook
from openpyxl.styles import colors, Fontdest_loc='/Users/abdulr06/Documents/Python Scripts/'np.seterr(divide='ignore', invalid='ignore')SRC='TSYS'
YM1='201707'dest_file=dest_loc+SRC+'_'+''+YM1+'.xlsx'  sheetname = [SRC+''+' GL-Recon']   #Following code is common for rest of the sourc systems 
wb=load_workbook(dest_file)fmtB=Font(color=colors.BLUE) 
fmtR=Font(color=colors.RED)for i in range(len(sheetname)):             sheet1=wb.get_sheet_by_name(sheetname[i])             print(sheetname[i])  last_record=sheet1.max_row+1 for m in range(2,last_record):if -30 <= sheet1.cell(row=m,column=5).value <=30:          ft=sheet1.cell(row=m,column=5)ft.font=fmtBft.number_format = '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)'ft1=sheet1.cell(row=m,column=6)ft1.number_format = '0.00%'else:          ft=sheet1.cell(row=m,column=5)  ft.font=fmtR  ft.number_format = '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)'ft1=sheet1.cell(row=m,column=6)ft1.number_format = '0.00%' 
wb.save(filename=dest_file)  

Exception:

Traceback (most recent call last):File "<ipython-input-17-fc16d9a46046>", line 6, in <module>wb.save(filename=dest_file)File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\workbook\workbook.py", line 263, in savesave_workbook(self, filename)File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 239, in save_workbookwriter.save(filename, as_template=as_template)File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 222, in saveself.write_data(archive, as_template=as_template)File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 80, in write_dataself._write_worksheets(archive)File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 163, in _write_worksheetsxml = sheet._write(self.workbook.shared_strings)File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\worksheet\worksheet.py", line 776, in _writereturn write_worksheet(self, shared_strings)File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\worksheet.py", line 263, in write_worksheetxf.write(worksheet.page_breaks.to_tree())File "serializer.pxi", line 1016, in lxml.etree._FileWriterElement.__exit__ (src\lxml\lxml.etree.c:141944)File "serializer.pxi", line 904, in lxml.etree._IncrementalFileWriter._write_end_element (src\lxml\lxml.etree.c:140137)File "serializer.pxi", line 999, in lxml.etree._IncrementalFileWriter._handle_error (src\lxml\lxml.etree.c:141630)File "serializer.pxi", line 195, in lxml.etree._raiseSerialisationError (src\lxml\lxml.etree.c:131006)SerialisationError: IO_WRITE
Answer

Why do you allocate font at each loop?

fmt=Font(color=colors.BLUE)

Or red, create two fonts red and blue, once and then use it, each time you are allocating Font, you are using more memory.

Optimise your code at first. Less code -> less errors, for example:

mycell = sheet1.cell(row=m,column=5)       
if -30 <= mycell.value <=30: mycell.font = redfont 

This should ensure that you do not have the issue again (hopefully)

https://en.xdnf.cn/q/118133.html

Related Q&A

How to stop the python turtle from drawing

Can anyone tell me why this code always has a line on the screen and also how to stop it?Slight problem with this is that every time this happens, I always get a line on my canvas no matter what I try…

Replace values in a string

So the challenge was to replace a specific word in a sentence with asterisks with equivalent length to that word - 3 letters 3 asterisks etc.Section One does not work, but Section Two does - can anyon…

Select n data points from plot

I want to select points by clicking om them in a plot and store the point in an array. I want to stop selecting points after n selections, by for example pressing a key. How can I do this? This is wha…

Python azure uploaded file content type changed to application/octet-stream

I am using python Azure sdk. When the file uploaded its content type changed to application/octet-stream. I want to set its default content type like image/png for PNG image.I am using following method…

the dumps method of itsdangerous throws a TypeError

I am following the guide of 『Flask Web Development』. I want to use itsdangerous to generate a token, but some problems occured. Here is my code:def generate_confirmation_token(self, expiration=3600):…

SP 500 List python script crashes

So I have been following a youtube tutorial on Python finance and since Yahoo has now closed its doors to the financial market, it has caused a few dwelling problems. I run this codeimport bs4 as bs im…

sleekxmpp threaded authentication

so... I have a simple chat client like so:class ChatClient(sleekxmpp.ClientXMPP):def __init__(self, jid, password, server):sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True)self.add_event_han…

DoxyPy - Member (variable) of namespace is not documented

I get the error message warning: Member constant1 (variable) of namespace <file_name> is not documented. for my doxygen (doxypy) documentation. I have documented the file and all functions and cl…

to_csv append mode is not appending to next new line

I have a csv called test.csv that looks like:accuracy threshold trainingLabels abc 0.506 15000 eew 18.12 15000And then a dataframe called summaryDF that looks like:accu…

Optional keys in string formats using % operator?

Is is possible to have optional keys in string formats using % operator? I’m using the logging API with Python 2.7, so I cant use Advanced String Formatting.My problem is as follow:>>> impor…