gpt4 book ai didi

python - XlsxWriter 设置全局字体大小

转载 作者:太空狗 更新时间:2023-10-30 02:16:27 26 4
gpt4 key购买 nike

如何在第一次创建工作簿时使用xlswriter永久设置字体大小?

我试过:

book = xlsxwriter.Workbook(os.getcwd() + '\\test.xlsx')
sheet1 = book.add_worksheet()
format = book.add_format()
format.set_font_size(10)

但我在输出中仍然得到默认大小 11。什么问题?

最佳答案

1,对于单个单元格的字体大小,你必须像这样将format传递给write:

workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Add a font size 10 format.
format = workbook.add_format()
format.set_font_size(10)
# Write some simple text.
worksheet.write('A1', 'Hello', format)

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)


workbook.close()

Hello 将设置为字体大小 10

enter image description here

更新:2、所有单元格的字体大小,可以设置工作簿的默认格式:

import xlsxwriter
workbook = xlsxwriter.Workbook('demo.xlsx')

# default cell format to size 10
workbook.formats[0].set_font_size(10)

worksheet = workbook.add_worksheet()


# Write some simple text.
worksheet.write('A1', 'Hello')

worksheet.write('A2', 'World')

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)

workbook.close()

所有单元格都将变为 10 号字体:

enter image description here

关于python - XlsxWriter 设置全局字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43991505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com