openpyxl – adjust column width size

You could estimate (or use a mono width font) to achieve this. Let’s assume data is a nested array like [[‘a1′,’a2’],[‘b1′,’b2’]] We can get the max characters in each column. Then set the width to that. Width is exactly the width of a monospace font (if not changing other styles at least). Even if you … 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.close() Here I … Read more

Is there a way to auto-adjust Excel column widths with pandas.ExcelWriter?

Inspired by user6178746’s answer, I have the following: # Given a dict of dataframes, for example: # dfs = {‘gadgets’: df_gadgets, ‘widgets’: df_widgets} writer = pd.ExcelWriter(filename, engine=”xlsxwriter”) for sheetname, df in dfs.items(): # loop through `dict` of dataframes df.to_excel(writer, sheet_name=sheetname) # send df to writer worksheet = writer.sheets[sheetname] # pull worksheet object for idx, col … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)