Windows function to use NamedTemporaryFile
After some research I found that tempfile.NamedTemporaryFile is broken on Windows (you could have a temporary file, or you could get a filename - but you cannot have both).


This is just flat out wrong. Maybe you're misunderstanding something and conflating two things. But this is just factually incorrect. And can be seen in your code:

temp_pptx.close()  # needed on windows systems to access file
prs.save(temp_pptx.name)


The first line is closing the temporary file, and the second is getting it's filename.

  needed on windows systems to access file


This is due to a security feature.


  The file is created securely, using the same rules as mkstemp().





  Creates a temporary file in the most secure manner possible. There are no race conditions in the file’s creation, assuming that the platform properly implements the os.O_EXCL flag for os.open(). The file is readable and writable only by the creating user ID. If the platform uses permission bits to indicate whether a file is executable, the file is executable by no one. The file descriptor is not inherited by child processes.


Clearly you don't need this, so tempfile is pretty much useless to you and you will be fighting it.
Your code looks very much like a mix of an exploit of how generators and garbage collection work, and some dodgy monkey patch to get around tempfile. Both are on a level of wtf/m that I'd suggest just writing your own temporary file class.


import os


class TemporaryFile:
    __slots__ = ('_file',)

    def __init__(self, file):
        self._file = file

    @classmethod
    def open(cls, *args, **kwargs):
        return cls(open(*args, **kwargs))

    def __enter__(self):
        return self._file.__enter__()

    def __exit__(self, exc_type, exc_value, traceback):
        ret = self._file.__exit__(exc_type, exc_value, traceback)
        os.remove(self._file.name)
        return ret


if __name__ == '__main__':
    with TemporaryFile.open('foo.txt', 'w+') as f:
        pass
    with TemporaryFile(open('foo.txt', 'w+')) as f:
        pass


I can write to the file using Atom using the above. Even if Excel has more security operations, then you can easily work around them without monkey patches.