openpyxl设置chart标签rotation=90
要设置图表标签的旋转角度为90度,可以使用openpyxl库的LabelRotation类。下面是一个示例代码:
from openpyxl import Workbook
from openpyxl.chart import BarChart
from openpyxl.drawing.text import LabelRotation
# 创建一个工作簿和一个工作表
wb = Workbook()
ws = wb.active
# 添加一些数据到工作表
data = [
['Month', 'Sales'],
['Jan', 150],
['Feb', 200],
['Mar', 100],
['Apr', 250],
]
for row in data:
ws.append(row)
# 创建一个柱形图
chart = BarChart()
chart.title = "Sales by Month"
chart.x_axis.title = "Month"
chart.y_axis.title = "Sales"
# 设置图表标签的旋转角度为90度
chart.x_axis.txPr = LabelRotation(angle=90)
# 添加数据到图表
chart.add_data(ws['B2:B5'], titles_from_data=True)
chart.set_categories(ws['A2:A5'])
# 将图表添加到工作表
ws.add_chart(chart, "E5")
# 保存工作簿
wb.save("chart_with_rotation.xlsx")
在上面的示例中,我们创建了一个柱形图,并将图表标签的旋转角度设置为90度。然后,我们将图表添加到工作表,并保存工作簿为一个Excel文件。
原文地址: https://www.cveoy.top/t/topic/jdgW 著作权归作者所有。请勿转载和采集!