Skip to content

Backport PR #39605: REGR: appending to existing excel file created corrupt files #39659

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Fixed regressions
- Fixed regression in :meth:`~DataFrame.to_pickle` failing to create bz2/xz compressed pickle files with ``protocol=5`` (:issue:`39002`)
- Fixed regression in :func:`pandas.testing.assert_series_equal` and :func:`pandas.testing.assert_frame_equal` always raising ``AssertionError`` when comparing extension dtypes (:issue:`39410`)
- Fixed regression in :meth:`~DataFrame.to_csv` opening ``codecs.StreamWriter`` in binary mode instead of in text mode and ignoring user-provided ``mode`` (:issue:`39247`)
- Fixed regression in :meth:`~DataFrame.to_excel` creating corrupt files when appending (``mode="a"``) to an existing file (:issue:`39576`)
- Fixed regression in :meth:`DataFrame.transform` failing in case of an empty DataFrame or Series (:issue:`39636`)
- Fixed regression in :meth:`core.window.rolling.Rolling.count` where the ``min_periods`` argument would be set to ``0`` after the operation (:issue:`39554`)
-
Expand Down
5 changes: 5 additions & 0 deletions pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from distutils.version import LooseVersion
import mmap
from typing import TYPE_CHECKING, Dict, List, Optional

import numpy as np
Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(
from openpyxl import load_workbook

self.book = load_workbook(self.handles.handle)
self.handles.handle.seek(0)
else:
# Create workbook object with default optimized_write=True.
self.book = Workbook()
Expand All @@ -50,6 +52,9 @@ def save(self):
Save workbook to disk.
"""
self.book.save(self.handles.handle)
if "r+" in self.mode and not isinstance(self.handles.handle, mmap.mmap):
# truncate file to the written content
self.handles.handle.truncate()

@classmethod
def _convert_to_style_kwargs(cls, style_dict: dict) -> Dict[str, "Serialisable"]:
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/io/excel/test_openpyxl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from distutils.version import LooseVersion
from pathlib import Path

import numpy as np
import pytest
Expand Down Expand Up @@ -149,3 +150,22 @@ def test_read_with_bad_dimension(datapath, ext, header, expected_data, filename)
result = pd.read_excel(path, header=header)
expected = DataFrame(expected_data)
tm.assert_frame_equal(result, expected)


def test_append_mode_file(ext):
# GH 39576
df = DataFrame()

with tm.ensure_clean(ext) as f:
df.to_excel(f, engine="openpyxl")

with ExcelWriter(f, mode="a", engine="openpyxl") as writer:
df.to_excel(writer)

# make sure that zip files are not concatenated by making sure that
# "docProps/app.xml" only occurs twice in the file
data = Path(f).read_bytes()
first = data.find(b"docProps/app.xml")
second = data.find(b"docProps/app.xml", first + 1)
third = data.find(b"docProps/app.xml", second + 1)
assert second != -1 and third == -1