I am trying to get the following code to work:
import polars as pl
# Create sample DataFrames
df1 = pl.DataFrame({
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35]
})
df2 = pl.DataFrame({
"Product": ["Laptop", "Phone", "Tablet"],
"Price": [1000, 500, 300]
})
df3 = pl.DataFrame({
"City": ["New York", "San Francisco", "Chicago"],
"Population": [8_400_000, 873_965, 2_746_388]
})
def openwb():
return xlsxwriter.workbook("name.xlsx")
def writewb(wb, df, sheetname):
df.write_excel(workbook=wb, worksheet=sheetname)
def main():
mywb = openwb()
writewb(mywb, df1, "s1")
writewb(mywb, df2, "s2")
writewb(mywb, df3, "s3")
Problem is that each worksheet deletes the previously written ones, leaving me with only worksheet s3 in my workbook.
Of course, this is oversimplified code. In reality, the functions above do a lot more stuff, and writing the worksheets is just one of the actions.
Since the calls are spread across function calls, I am not using the "with workbook as wb ..." approach, since I feel that won't leave the workbook open across the function calls.
How do I solve this? It seems I can convert the dataframe to pandas, but I am hoping for a polars-native solution
The workbook object needs to be closed. This is usually best achieved with a with
context manager like in this polars/xlsxwriter example.
However, you can also call an explicit close()
on the workbook. Like this:
import polars as pl
import xlsxwriter
# Create sample DataFrames
df1 = pl.DataFrame({
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35]
})
df2 = pl.DataFrame({
"Product": ["Laptop", "Phone", "Tablet"],
"Price": [1000, 500, 300]
})
df3 = pl.DataFrame({
"City": ["New York", "San Francisco", "Chicago"],
"Population": [8_400_000, 873_965, 2_746_388]
})
def openwb():
return xlsxwriter.Workbook("name.xlsx")
def writewb(wb, df, sheetname):
df.write_excel(workbook=wb, worksheet=sheetname)
def main():
mywb = openwb()
writewb(mywb, df1, "s1")
writewb(mywb, df2, "s2")
writewb(mywb, df3, "s3")
mywb.close()
if __name__ == "__main__":
main()
Output: