Reputation: 2441
I have a Final.xlsx
that contains multiple sheet - shee1, sheet2 ,sheet3
, each having some graphs and data. I have another file file5.xlsx
that i want to add in Final.xlsx
in tab . The below code is working but the Final.xlsx
existing sheets data is getting missed(contents,formats, grpahs, and others) . need help to fix this.
import pandas
from openpyxl import load_workbook
book = load_workbook('foo.xlsx')
writer = pandas.ExcelWriter('foo.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df1=pd.read_excel('file5.xlsx')
df1.to_excel(writer, "new",index=False)
writer.save()
Upvotes: 0
Views: 435
Reputation: 19497
Internally Pandas uses the xlrd library to read xlsx files. This library is fast but, because it is essentially bolted onto support for the BIFF format, it's support for OOXML is limited. Seeing as Pandas doesn't know anything about charts, it couldn't keep them anyway.
openpyxl provides utilities in openpyxl.utils.dataframe
for going between XLSX's rows and Pandas Dataframes giving you full control when working, while keeping nearly everything else in your file. In your case, however, you don't even need Pandas as you can simply loop over the cells from "file5.xlsx" and copy them to your other file.
Upvotes: 1