Closed TomHsiung closed 3 months ago
Thanks for the request. I do not think pandas can support Excel features like this in maintainable manner. I recommend having a function to add the drop-down list after writing to the Excel file if it needs to happen multiple times.
Thank you for your update, pal. Yep, there are some methods to achieve the goal. I was thinking of a more native way, using only pandas to do this. Anyway, many thanks for your clarification.
import pandas as pd import numpy as np from openpyxl import load_workbook from openpyxl.utils.dataframe import dataframe_to_rows
Load existing excel file
wb = load_workbook('/Users/username/food_1Aug2024.xlsx') ws = wb.active
Add new rows via pandas
new_rows = pd.DataFrame({ 'Month': [8, 8, 8, 8, 8], 'Day': [7, 7, 7, 7, 7], 'Cost': [20.8, 18.99, 3.99, 35.48, 2.98], 'Category': ['Care', 'Meat', 'Veg', 'Meat', 'Veg'], 'Quarter': [3, 3, 3, 3, 3], })
Convert the DataFrame to rows of data
for r in dataframe_to_rows(new_rows, index = False, header = False): ws.append(r)
Save updated data to a new excel file
wb.save('/Users/username/food_7Aug2024.xlsx')
Agreed that it wouldn't be suitable to maintain this feature in pandas so closing
Feature Type
[X] Adding new functionality to pandas
[ ] Changing existing functionality in pandas
[ ] Removing existing functionality in pandas
Problem Description
I write data into a new xlsx table via pandas. Later, I add drop list menu function by openpyxl. But, if I want to add more rows later by pandas again, like this,
The pre-defined drop list menu function is lost, after the edited xlsx file is saved by pandas.
Feature Description
Maybe it is a reasonable function to add pandas's self features to support drop-down list menu.
Alternative Solutions
Or, maybe it is reasonable to enable pandas's support for the drop-down list menu function of openpyxl
Additional Context
Thanks!