From d13c2bccfbe7ad4475ce7a2a9da45b76c36d9697 Mon Sep 17 00:00:00 2001 From: Zoynels <5841616+Zoynels@users.noreply.github.com> Date: Sat, 3 Aug 2019 22:07:05 +0600 Subject: [PATCH 1/3] Add: update dict of sheets before check ```python import pandas as pd df = pd.DataFrame( { "id": ["1", "2", "3", "4", "5"], "Feature1": ["A", "C", "E", "G", "I"], "Feature2": ["B", "D", "F", "H", "J"], }, columns=["id", "Feature1", "Feature2"], ) writer = pd.ExcelWriter(path="testOutput.xlsx", mode="a", engine="openpyxl") df.to_excel(writer, sheet_name="Main") writer.save() writer.close() ``` #### Problem description I have excel file with existing sheet "Main", and if I try to append some dataframe to this sheet, on first df.to_excel() pandas creates new sheet with name "Main1" and saves information to this new sheet. But if once to_excel(), than pandas add new sheet to dictionary and saves to existing sheet. So, on start appen pandas don't know nothing about existing sheets, and check in **_openpyxl.py** file don't realy work ```python if sheet_name in self.sheets: wks = self.sheets[sheet_name] else: wks = self.book.create_sheet() wks.title = sheet_name self.sheets[sheet_name] = wks ``` so I add new code to update sheetname-list before check. --- pandas/io/excel/_openpyxl.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/io/excel/_openpyxl.py b/pandas/io/excel/_openpyxl.py index d8f5da5ab5bc6..a0ff1bf640278 100644 --- a/pandas/io/excel/_openpyxl.py +++ b/pandas/io/excel/_openpyxl.py @@ -397,6 +397,11 @@ def write_cells( _style_cache = {} + # Update sheet list + self.sheets = {} + for wks in self.book.worksheets: + self.sheets[wks.title] = wks + if sheet_name in self.sheets: wks = self.sheets[sheet_name] else: From 09f31bc780e41996e91713822368b8f1771e9e6f Mon Sep 17 00:00:00 2001 From: Zoynels <5841616+Zoynels@users.noreply.github.com> Date: Wed, 7 Aug 2019 01:24:20 +0600 Subject: [PATCH 2/3] add tests --- pandas/tests/io/excel/test_openpyxl.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/tests/io/excel/test_openpyxl.py b/pandas/tests/io/excel/test_openpyxl.py index 79fc87a62ad08..87c6f3a0ce198 100644 --- a/pandas/tests/io/excel/test_openpyxl.py +++ b/pandas/tests/io/excel/test_openpyxl.py @@ -78,10 +78,11 @@ def test_write_cells_merge_styled(ext): @pytest.mark.parametrize( - "mode,expected", [("w", ["baz"]), ("a", ["foo", "bar", "baz"])] + "mode,expected", [("w", ["new_sheet"]), ("a", ["foo", "bar", "existing_sheet", "new_sheet"])] ) def test_write_append_mode(ext, mode, expected): - df = DataFrame([1], columns=["baz"]) + df_new_sheet = DataFrame([1], columns=["new_sheet"]) + df_existing_sheet = DataFrame([1], columns=["existing_sheet"]) with ensure_clean(ext) as f: wb = openpyxl.Workbook() @@ -89,10 +90,13 @@ def test_write_append_mode(ext, mode, expected): wb.worksheets[0]["A1"].value = "foo" wb.create_sheet("bar") wb.worksheets[1]["A1"].value = "bar" + wb.create_sheet("existing_sheet") wb.save(f) writer = ExcelWriter(f, engine="openpyxl", mode=mode) - df.to_excel(writer, sheet_name="baz", index=False) + if mode == "a": + df_existing_sheet.to_excel(writer, sheet_name="existing_sheet", index=False) + df_new_sheet.to_excel(writer, sheet_name="new_sheet", index=False) writer.save() wb2 = openpyxl.load_workbook(f) From 419e29417b688d03fee9b94ac707967fc9026596 Mon Sep 17 00:00:00 2001 From: Zoynels <5841616+Zoynels@users.noreply.github.com> Date: Wed, 7 Aug 2019 19:24:44 +0600 Subject: [PATCH 3/3] PEP 8 change --- pandas/tests/io/excel/test_openpyxl.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/excel/test_openpyxl.py b/pandas/tests/io/excel/test_openpyxl.py index 87c6f3a0ce198..c19376b1a3438 100644 --- a/pandas/tests/io/excel/test_openpyxl.py +++ b/pandas/tests/io/excel/test_openpyxl.py @@ -78,7 +78,8 @@ def test_write_cells_merge_styled(ext): @pytest.mark.parametrize( - "mode,expected", [("w", ["new_sheet"]), ("a", ["foo", "bar", "existing_sheet", "new_sheet"])] + "mode,expected", [("w", ["new_sheet"]), + ("a", ["foo", "bar", "existing_sheet", "new_sheet"])] ) def test_write_append_mode(ext, mode, expected): df_new_sheet = DataFrame([1], columns=["new_sheet"])