Write to StringIO object using Pandas Excelwriter?

Pandas expects a filename path to the ExcelWriter constructors although each of the writer engines support StringIO. Perhaps that should be raised as a bug/feature request in Pandas. In the meantime here is a workaround example using the Pandas xlsxwriter engine: import pandas as pd import StringIO io = StringIO.StringIO() # Use a temp filename … Read more

pandas xlsxwriter, format table header – not sheet header

I think you need first reset default header style, then you can change it: pd.core.format.header_style = None All together: import pandas as pd data = pd.DataFrame({‘test_data’: [1,2,3,4,5]}) writer = pd.ExcelWriter(‘test.xlsx’, engine=”xlsxwriter”) pd.core.format.header_style = None data.to_excel(writer, sheet_name=”test”, index=False) workbook = writer.book worksheet = writer.sheets[‘test’] font_fmt = workbook.add_format({‘font_name’: ‘Arial’, ‘font_size’: 10}) header_fmt = workbook.add_format({‘font_name’: ‘Arial’, ‘font_size’: 10, … Read more

Adjust cell width in Excel

You could use set_column as follows: worksheet1.set_column(1, 1, 25) This is defined as follows: set_column(first_col, last_col, width, cell_format, options) You would need to determine a suitable width, perhaps based on the longest length of text in the whole column. Care though would be needed to base this on the font and size being used. Also … Read more

How to write/update data into cells of existing XLSX workbook using xlsxwriter in python

Quote from xlsxwriter module documentation: This module cannot be used to modify or write to an existing Excel XLSX file. If you want to modify existing xlsx workbook, consider using openpyxl module. See also: Modify an existing Excel file using Openpyxl in Python Use openpyxl to edit a Excel2007 file (.xlsx) without changing its own … Read more

Putting many python pandas dataframes to one excel worksheet

To create the Worksheet in advance, you need to add the created sheet to the sheets dict: writer.sheets[‘Validation’] = worksheet Using your original code: # Creating Excel Writer Object from Pandas writer = pd.ExcelWriter(‘test.xlsx’,engine=”xlsxwriter”) workbook=writer.book worksheet=workbook.add_worksheet(‘Validation’) writer.sheets[‘Validation’] = worksheet df.to_excel(writer,sheet_name=”Validation”,startrow=0 , startcol=0) another_df.to_excel(writer,sheet_name=”Validation”,startrow=20, startcol=0) Explanation If we look at the pandas function to_excel, it uses … Read more

How to save a new sheet in an existing excel file, using Pandas?

Thank you. I believe that a complete example could be good for anyone else who have the same issue: import pandas as pd import numpy as np path = r”C:\Users\fedel\Desktop\excelData\PhD_data.xlsx” x1 = np.random.randn(100, 2) df1 = pd.DataFrame(x1) x2 = np.random.randn(100, 2) df2 = pd.DataFrame(x2) writer = pd.ExcelWriter(path, engine=”xlsxwriter”) df1.to_excel(writer, sheet_name=”x1″) df2.to_excel(writer, sheet_name=”x2″) writer.save() writer.close() Here … Read more

tech