Adjust cell width in Excel

0 votes

I'm writing into an Excel sheet using xlsxwriter. The problem I'm having is that text that exceeds the size of the cell is concealed.

import xlsxwriter

workbook = xlsxwriter.Workbook("file.xlsx")
worksheet1 = workbook.add_worksheet()

worksheet1.write(1, 1,"long text hidden test-1" )
worksheet1.write(2, 1,"long text hidden test-2")
worksheet1.write(3, 1,"short-1")
worksheet1.write(4, 1,"short-2")
worksheet1.write(1, 2,"Hello world" )
worksheet1.write(2, 2,"Hello world")
worksheet1.write(3, 2,"Hello world")
worksheet1.write(4, 2,"Hello world")

workbook.close()

What I am getting

enter image description here

What I am expecting with adjusted widths

enter image description here

Oct 10 in Others by Kithuzzz
• 20,660 points
61 views

1 answer to this question.

0 votes

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)

If you want to autofit all of the columns automatically regardless of the font and size, then you will need to use the win32com interface as follows:

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r'file.xlsx')
ws = wb.Worksheets("Sheet1")
ws.Columns.AutoFit()
wb.Save()
excel.Application.Quit()
answered Oct 10 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer

Change date format of cell in excel from dd.mm.yyyy to yyy/mm/dd ( excel version 2013 )

Hello :)   Excel’s Format Cells function can quickly ...READ MORE

answered Feb 9 in Others by gaurav
• 22,040 points
544 views
0 votes
1 answer
0 votes
1 answer

Absolute worksheet reference in excel cell formulas

Try this: =INDIRECT("Sheet1!C"&ROW()) READ MORE

answered Oct 10 in Others by narikkadan
• 37,660 points
59 views
0 votes
1 answer

How to express absolute cell references in Excel R1C1-style formulas?

If the formulas are in Column K ...READ MORE

answered Oct 15 in Others by narikkadan
• 37,660 points
52 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,149 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Change the size of one cell in excel 2016

I'm sorry, but it's not feasible. A ...READ MORE

answered Oct 8 in Others by narikkadan
• 37,660 points
50 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP