Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-memory workbooks are not compressed on close() #573

Closed
theag3nt opened this issue Oct 18, 2018 · 3 comments · Fixed by #574
Closed

In-memory workbooks are not compressed on close() #573

theag3nt opened this issue Oct 18, 2018 · 3 comments · Fixed by #574
Assignees
Labels

Comments

@theag3nt
Copy link
Contributor

theag3nt commented Oct 18, 2018

Using XlsxWriter with the in_memory option results in files which are not properly compressed.

The issue is caused by XlsxWriter constructing its own ZipInfo objects and using ZipFile.writestr() to write them without specifying the compression type. It's not documented, but from the Python zipfile module's source I was able to tell that in this case ZipInfo files do not inherit the ZipFile's compression and default to ZIP_STORED. As the ZipFile is instantiated with ZIP_DEFLATED I assume this is not intentional.

I've verified the problem occurs with XlsxWriter 1.1.1 (and the latest development version too) both with Python version 2.7.15 and 3.6.6.

A short script that can be used to reproduce the problem:

from xlsxwriter import Workbook

with open("sample.xlsx", "wb") as f, Workbook(f, {"in_memory": True}) as wb:
    ws = wb.add_worksheet()
    ws.write_number("A1", 0)

I've first noticed the size differences when comparing generated files with ones created by Excel. But it can be verified with the zipinfo command:

$ zipinfo sample.xlsx
Archive:  sample.xlsx
Zip file size: 13503 bytes, number of entries: 9
?rw-------  2.0 unx      516 b- stor 80-Jan-01 00:00 xl/worksheets/sheet1.xml
?rw-------  2.0 unx      550 b- stor 80-Jan-01 00:00 xl/workbook.xml
?rw-------  2.0 unx      784 b- stor 80-Jan-01 00:00 docProps/app.xml
?rw-------  2.0 unx      592 b- stor 80-Jan-01 00:00 docProps/core.xml
?rw-------  2.0 unx     1031 b- stor 80-Jan-01 00:00 [Content_Types].xml
?rw-------  2.0 unx      867 b- stor 80-Jan-01 00:00 xl/styles.xml
?rw-------  2.0 unx     6994 b- stor 80-Jan-01 00:00 xl/theme/theme1.xml
?rw-------  2.0 unx      587 b- stor 80-Jan-01 00:00 _rels/.rels
?rw-------  2.0 unx      556 b- stor 80-Jan-01 00:00 xl/_rels/workbook.xml.rels
9 files, 12477 bytes uncompressed, 12477 bytes compressed:  0.0%

The expected compression level is greater than 0%, also the files are listed with STORE compression.

@jmcnamara
Copy link
Owner

Thanks for the detailed bug report and investigation. I'll do some extra testing and merge up the PR.

John

@jmcnamara jmcnamara self-assigned this Oct 19, 2018
@jmcnamara jmcnamara added the bug label Oct 19, 2018
jmcnamara pushed a commit that referenced this issue Oct 20, 2018
@jmcnamara
Copy link
Owner

Fixed in version 1.1.2.

Thanks.

@theag3nt
Copy link
Contributor Author

Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants