So here's the code I wrote to change the font in my xlsx file output:
wb = xw.Book('aapl.xlsx')
sht = wb.sheets('Sheet1')
last_cell = sht.range("A1").end(Direction.xlDown).end(Direction.xlToRight)
print('Last cell at: (' + str(last_cell.column) + ', ' + str(last_cell.row) + ')' + '\n')
for i in range(1, last_cell.column):
for j in range(1, last_cell.row):
sht.cells(i, j).api.Font.Name = 'JetBrains Mono'
for ws in wb.sheets:
ws.autofit(axis='columns')
wb.save()
and the problem here is: I want to set the font type to JetBrains Mono
in my file, but I don't know why it can only change to sixth row. I have tried to print out the location of last_cell
to see if I made any mistakes, and also swapped out last_row.column
in the second for loop to see if range(1, 107)
(107 is the row count in my file) works. But, still ended up with this unfortunately. how the file I ended up getting looks like
There are a few things you can simplify in your code:
font.name
without having to use the api
workaroundexpand
or current_region
should make your life easierThis code should be all you need:
wb = xw.Book('aapl.xlsx')
sht = wb.sheets('Sheet1')
sht['A1'].expand().font.name = 'JetBrains Mono'