Unable to save valid Excel workbook using Django admin
Created originally on Bitbucket by apolosky (Alex)
I'm having an issue generating a valid workbook when executing a Django admin command. I've isolated the issue to be specifically when using a command; the issue does not seem to appear if I generate a workbook as a downloadable page in Django.
The issue occurs both when is_write_only is set to True or False, and different behavior occurs upon each. I'm not sure if this is a bug that has occurred in the past and I'm just not setting something properly, however I did try searching across django issues and didn't really find anything. Let me know if I should crosspost this to the Django issues as well.
The issue that occurs:
is_write_only=True Workbook is generated; when opening the workbook Excel generates an error message "We found a problem with some content in 'django_issue.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.". Clicking yes will recover all data that was contained, however any formulas used (not in the example) will be stripped out. Clicking no will not recover any data.
is_write_only=False Workbook is generated; when opening the workbook Excel generates an error message "We found a problem with some content in 'django_issue.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.". Clicking yes will recover all data that was contained, however any formulas used (not in the example) will be stripped out. In addition, an additional sheet titled 'Sheet' will appear as the first tab; I think that is expected behavior however as that sheet will only be not generated when using is_write_only=True. Clicking no will not recover any data.
I've copied the minimum code required below to replicate the issue, and if required I can paste the full command that I have if required.
In addition I've attached the workbooks that I generated with this code, but modified the names to give them context. If required, I can also upload a workbook generated from the context of Django when running the server rather than a command (although this workaround is really not efficient for the purposes that I am using it for).
#!python
import openpyxl
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
def add_arguments(self, parser):
pass
def handle(self, *args, **options):
is_write_only = True # or False
wb = openpyxl.Workbook(write_only=is_write_only)
ws = wb.create_sheet('index')
for i in range(200):
ws.append([i])
wb.save(os.path.join(os.getcwd(), 'django_issue.xlsx'))
wb.close()
Attachments: django_issue.write_only_false.xlsxdjango_issue.write_only_true.xlsxdjango_issue.server.xlsx