I have an Excel file (Celebrities.xlsx) with multiple sheets and I'm trying to modify a single sheet called Relationships without modifying (or potentially erasing) other sheets. Here's what I've done.
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
# Name of the celebrity that I want to modify
celeb_name = 'Terence Stamp'
wb = load_workbook('Celebrities.xlsx')
ws = wb['Relationships']
df = pd.read_excel('Celebrities.xlsx', sheet_name='Relationships')
# This part is trivial, but basically I'm replacing every null cell in 'Link' column with the word 'empty' (of that particular celebrity)
df.loc[(df['Celebrity Name'] == celeb_name) & (df['Link'].isnull()), 'Link'] = 'empty'
for r in dataframe_to_rows(df, index=True, header=True):
ws.append(r)
wb.save('new.xlsx')
Now the script runs without any error and new.xlsx is created successfully, but when I try to open it, it gives me this error:
Warning loading document new.xlsx: The data could not be loaded completely because the maximum number of rows per sheet was exceeded.
And nothing has been modified!
I can assure that this part of the code works perfectly:
wb = load_workbook('Celebrities.xlsx')
ws = wb['Relationships']
wb.save('new.xlsx')
I suppose the problem is with this part of code:
for r in dataframe_to_rows(df, index=True, header=True):
ws.append(r)
But I don't know how to fix it.
load_workbook()andpandas.read_excel()on the same file.Pandasand I modify the file withopenpyxl.