You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用XlsxWriter设置文本框内文字为垂直方向?

Textbox Vertical Text in XlsxWriter

Got it, let's tackle this textbox vertical text issue you're facing with XlsxWriter. You already have the hang of rotating cell text, but textboxes work a bit differently since they're shape objects rather than standard cells. The insert_textbox method doesn't have a direct vertical text parameter in its options, but there are two solid workarounds to get the effect you want:

1. Simulate Vertical Text with Line Breaks

If you just want text stacked top-to-bottom, split each character onto its own line using \n. Pair this with your existing center alignment for clean, straightforward vertical output:

text = '\n'.join('Some text in a textbox')
options = {
    'font': {'color': 'white'},
    'align': {'vertical': 'middle', 'horizontal': 'center'},
    'gradient': {'colors': ['red', 'blue']},
}
# Replace row/col with your target position
worksheet.insert_textbox(row, col, text, options)

2. Use Shape Text Rotation (Native Excel Style)

For a more polished, native Excel vertical text effect (where text is rotated 90/270 degrees instead of just line-broken), leverage the Shape object returned by insert_textbox. You can call set_text_rotation() on it to rotate the text directly:

text = 'Some text in a textbox'
options = {
    'font': {'color': 'white'},
    'align': {'vertical': 'middle', 'horizontal': 'center'},
    'gradient': {'colors': ['red', 'blue']},
}
# Capture the textbox shape object when inserting it
textbox = worksheet.insert_textbox(row, col, text, options)
# Rotate 90 degrees (text reads upward) or 270 degrees (text reads downward)
textbox.set_text_rotation(90)

Unlike cell text rotation (where you apply a format to the cell or column), textboxes are shape elements—so we modify their properties directly right after creation.

内容的提问来源于stack exchange,提问作者Богдан Гергель

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:27:42