I want to put all row heights to 20 and all column widths to 30. I tried this code: All is just with pandas dataframe.
JavaScript
x
5
1
worksheet2.set_default_row(20) #it works but in Excel is it 19.80
2
worksheet2.set_default_column(30) #it doesn't work,
3
4
Error: 'Worksheet' object has no attribute 'set_default_column'
5
Advertisement
Answer
Creating excel file
JavaScript
1
14
14
1
import pandas as pd
2
import numpy as np
3
4
# creating and saving excel file
5
df = pd.DataFrame(
6
np.random.randint(0, 10, size=(10, 4)),
7
columns=['column name 1', 'column name 2',
8
'column name 3', 'column name 4']
9
)
10
writer = pd.ExcelWriter('test_file.xlsx')
11
df.to_excel(writer, sheet_name='my_sheet', index=False)
12
writer.save()
13
14
Changing size of columns and rows
JavaScript
1
17
17
1
import pandas as pd
2
3
df = pd.read_excel('test_file.xlsx')
4
writer = pd.ExcelWriter('test_file.xlsx')
5
df.to_excel(writer, sheet_name='my_sheet', index=False)
6
7
worksheet = writer.sheets['my_sheet']
8
9
# change column width of all columns
10
worksheet.set_column(0, len(df), 30)
11
12
# change row height
13
for row in range(0, len(df.index)+1):
14
worksheet.set_row(row, 20)
15
16
writer.save()
17
Before
After
Reference
https://xlsxwriter.readthedocs.io/worksheet.html
You might also want to take a look at this to dynamically change the row height and column width