导出到 excel 时,我似乎无法旋转熊猫数据帧标题单元格中的字符串。我使用了命令
rot_format = workbook.add_format()
rot_format.set_rotation(90)
rot_format.set_text_wrap()
worksheet.set_row(0,200,rot_format)
顶行单元格中的字符串不会旋转。但是,当我将set_row行中的"0"替换为"1"时,电子表格第二行单元格中的元素都会旋转。如果需要,我可以发送更多代码。
我很乐意考虑的另一种形式是将标签垂直书写一个字符宽,一个字符宽,一个在另一个字符下方。
任何见解将不胜感激。
谢谢。
彼得。
我找到了解决方法。我对标题所做的任何事情都不起作用,所以我写到没有标题的 excel 并在写入文档时跳过一行:
即:
doc.to_excel(writer, startrow = 2, sheet_name='Sheet', index=False,
header=False)
#Defined the format:
header_fmt = workbook.add_format({'align': 'bottom', 'rotation': '90',
'bold': True})
#Then I copied the names of the columns into a list:
header_names = doc.columns.tolist()
col_length = len(header_names)
#And wrote it to the header row that I skipped and assigned the format
#defined above.
for j in range(col_length):
col = j
worksheet.write(1, col, header_names[j], header_fmt)