Too many files open when adding sheet to write only book

Issue #863 resolved
Jon Mitten
created an issue

Here's the code:

I'm calling my class like this:

    reporter = XLSReport(title='last_timestamp',
                         sheet_name='timestamps',
                         status_report=status_report,
                         cols=[
                               'Unit ID',
                               'Status Timestamp',
                               'Days Since Last Report'
                         ],
                         highlight_dev=True,
                         )
    reporter.generate_report()
    reporter.save_report()
from datetime import datetime
from lister import lister
from openpyxl import Workbook


class XLSReport:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
        self.wb = Workbook(write_only=True)
        self.ts = datetime.utcnow()
        self.ws = self.wb.create_sheet()
        self.dev_units = list(lister('dev_pt_units.txt'))

    def generate_headers(self):
        if self.cols:
            cols = self.cols
        else:
            cols = ['col 1', 'col 2', 'col 3']
        # self.ws.append(cols)
        return cols

    def generate_report(self):
        stats = self.status_report
        headers = self.generate_headers()
        self.ws.append(headers)
        for stat in stats:
            if self.highlight_dev:
                dev_ws = self.wb.create_sheet()
                dev_ws.title = 'Dev Units'
                dev_headers = self.generate_headers()
                dev_ws.append(dev_headers)
                if stat[0] in self.dev_units:
                    report_stat = stat
                    dev_ws.append(report_stat)
            else:
                print('stat = {}\n\n'.format(stat))
                report_stat = stat
                self.ws.append(report_stat)

    def save_report(self):
        self.wb.save('Timestamp Report - ' + str(self.ts) + '.xlsx')

and the output:

  File "/platformUtils/sd/XLSReport.py", line 33, in generate_report
  File "/sanity/lib/python3.5/site-packages/openpyxl/workbook/workbook.py", line 156, in create_sheet
  File "/sanity/lib/python3.5/site-packages/openpyxl/writer/write_only.py", line 66, in __init__
  File "/sanity/lib/python3.5/site-packages/openpyxl/writer/write_only.py", line 41, in create_temporary_file
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tempfile.py", line 549, in NamedTemporaryFile
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tempfile.py", line 260, in _mkstemp_inner
OSError: [Errno 24] Too many open files: '/var/folders/d1/5h6gm67s0q51t3sxsgnyzlxm0000gp/T/openpyxl.exdmk_ki'

As far as I can tell I'm applying the write_only option correctly, according to the docs. Is this my mistake? Apologies if it's an oversight on my part.

Comments (3)

  1. Log in to comment