Python Forum
Python code to set column width
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code to set column width
#10
(Jan-14-2024, 07:52 AM)1418 Wrote: Can you tell me what additional library I need, cheers

First you can try this answer
writer = pd.ExcelWriter('/path/to/output/file.xlsx') 
df.to_excel(writer, sheet_name='sheetName', index=False, na_rep='NaN')

for column in df:
    column_length = max(df[column].astype(str).map(len).max(), len(column))
    col_idx = df.columns.get_loc(column)
    writer.sheets['sheetName'].set_column(col_idx, col_idx, column_length)

writer.save()
which should adjust all your columns to a reasonable width, unless you have a really long text in them.

or manual way
col_idx = df.columns.get_loc('columnName')
writer.sheets['sheetName'].set_column(col_idx, col_idx, 15)
note that you need to use ExcelWriter from pandas in both examples

or install xlsxwriter or styleframe or openpyxl or whatever else they used in the example and do it that way
1418 likes this post
Reply


Messages In This Thread
Python code to set column width - by 1418 - Jan-14-2024, 03:31 AM
RE: Python code to set column width - by Larz60+ - Jan-14-2024, 04:47 AM
RE: Python code to set column width - by 1418 - Jan-14-2024, 05:01 AM
RE: Python code to set column width - by sgrey - Jan-14-2024, 05:44 AM
RE: Python code to set column width - by 1418 - Jan-14-2024, 06:22 AM
RE: Python code to set column width - by sgrey - Jan-14-2024, 07:05 AM
RE: Python code to set column width - by 1418 - Jan-14-2024, 07:41 AM
RE: Python code to set column width - by sgrey - Jan-14-2024, 07:48 AM
RE: Python code to set column width - by 1418 - Jan-14-2024, 07:52 AM
RE: Python code to set column width - by sgrey - Jan-14-2024, 07:56 AM
RE: Python code to set column width - by 1418 - Jan-20-2024, 04:48 AM
RE: Python code to set column width - by Pedroski55 - Jan-20-2024, 07:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fixed colum width for rowLabels i Matplotlib pandabay 0 492 Jun-10-2023, 03:40 PM
Last Post: pandabay
  Code for pullng all data in a column EmBeck87 5 1,210 Apr-03-2023, 03:43 PM
Last Post: deanhystad
  Reshaping a single column in to multiple column using Python sahar 7 2,174 Jun-20-2022, 12:35 PM
Last Post: deanhystad
  width of Unicode character Skaperen 6 2,897 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  image.thumbnail(width, height) not working PCesarano 2 3,554 Apr-08-2021, 06:09 PM
Last Post: PCesarano
  How can I get the width of a string in Python? aquerci 14 16,593 May-27-2019, 06:00 PM
Last Post: heiner55
  fixed width numbers Skaperen 15 8,982 May-27-2019, 09:42 AM
Last Post: Skaperen
  Code snippets for building multi column Listviews and or Treeview KevinBrown 3 3,386 Apr-14-2019, 06:50 PM
Last Post: Yoriz
  printing text tables with consistent width Skaperen 7 10,824 Jul-01-2018, 02:34 AM
Last Post: Skaperen
  How to measure an inclined beam width and height in image using python? zyb1003 1 3,328 Nov-07-2017, 05:02 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020