简体   繁体   English

保存Excel文件时发生I / O错误-Python

[英]I/O Error while saving Excel file - Python

Im using python to open an existing excel file and do some formatting and save and close the file. 我使用python打开现有的excel文件并进行一些格式化,然后保存并关闭该文件。 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. 文件大小较小但excel大小较大(大约40MB)时,我的代码运行良好。我遇到序列化I / O错误,由于内存问题或我的代码而无法确定。 Kindly help. 请帮助。

System Config: 系统配置:

RAM - 8 GB 32 - bit operation Windows 7 RAM-8 GB 32-Windows 7操作

Code: 码:

import numpy as np
from openpyxl import load_workbook
from openpyxl.styles import colors, Font


dest_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=fmtB
          ft.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 save
    save_workbook(self, filename)

  File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 239, in save_workbook
    writer.save(filename, as_template=as_template)

  File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 222, in save
    self.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_data
    self._write_worksheets(archive)

  File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 163, in _write_worksheets
    xml = sheet._write(self.workbook.shared_strings)

  File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\worksheet\worksheet.py", line 776, in _write
    return write_worksheet(self, shared_strings)

  File "C:\Users\abdulr06\AppData\Local\Continuum\Anaconda3\lib\site-packages\openpyxl\writer\worksheet.py", line 263, in write_worksheet
    xf.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

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. 或红色,分别创建红色和蓝色两种字体,然后使用它,每次分配Font时,您将使用更多的内存。

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) 这样可以确保您不再遇到问题(希望如此)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM