|
|
|
@ -36,7 +36,7 @@ if __name__ == '__main__':
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# 将测试数据写入 Excel 文件
|
|
|
|
|
write_to_excel(test_cases, 'bilibili_videos.xlsx')
|
|
|
|
|
write_to_excel(test_cases, 'bilibili_videos_with_charts.xlsx')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -82,36 +82,39 @@ def calculate_ratio_and_update(file_path, sheet_name):
|
|
|
|
|
wb.save(file_path)
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
# 示例用法
|
|
|
|
|
file_path = 'bilibili_videos.xlsx' # Excel 文件路径
|
|
|
|
|
file_path = 'bilibili_videos_with_charts.xlsx' # Excel 文件路径
|
|
|
|
|
sheet_name = 'Sheet' # 工作表名称
|
|
|
|
|
|
|
|
|
|
calculate_ratio_and_update(file_path, sheet_name)
|
|
|
|
|
print("Data analysis written to the Excel file.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
import openpyxl
|
|
|
|
|
from openpyxl.chart import ScatterChart, BarChart, AreaChart, Reference
|
|
|
|
|
from openpyxl.chart import BarChart, Reference, AreaChart
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_bar_chart(file_name, sheet_name):
|
|
|
|
|
# 读取Excel文件
|
|
|
|
|
wb = openpyxl.load_workbook('bilibili_videos.xlsx')
|
|
|
|
|
ws = wb.active
|
|
|
|
|
wb = openpyxl.load_workbook(file_name)
|
|
|
|
|
ws = wb[sheet_name]
|
|
|
|
|
|
|
|
|
|
# 创建柱状图
|
|
|
|
|
bar_chart = BarChart()
|
|
|
|
|
bar_chart.title = "观众对于视频的认可度"
|
|
|
|
|
bar_chart.y_axis.title = "Data"
|
|
|
|
|
bar_chart.x_axis.title = "Index"
|
|
|
|
|
|
|
|
|
|
# 设置柱状图数据
|
|
|
|
|
bar_data = Reference(ws, min_col=17, min_row=2, max_row=ws.max_row)
|
|
|
|
|
bar_categories = Reference(ws, min_col=2, min_row=2, max_row=ws.max_row)
|
|
|
|
|
bar_chart.add_data(bar_data, titles_from_data=True)
|
|
|
|
|
bar_chart.set_categories(bar_categories)
|
|
|
|
|
|
|
|
|
|
# 添加柱状图到工作表
|
|
|
|
|
ws.add_chart(bar_chart, "V1")
|
|
|
|
|
|
|
|
|
|
# 创建柱状图1
|
|
|
|
|
bar_chart1 = BarChart()
|
|
|
|
|
bar_chart1.title = "观众对于视频的认可度"
|
|
|
|
|
bar_chart1.y_axis.title = "Data"
|
|
|
|
|
bar_chart1.x_axis.title = "Index"
|
|
|
|
|
|
|
|
|
|
# 设置柱状图1数据
|
|
|
|
|
bar_data1 = Reference(ws, min_col=17, min_row=2, max_row=ws.max_row)
|
|
|
|
|
bar_categories1 = Reference(ws, min_col=2, min_row=2, max_row=ws.max_row)
|
|
|
|
|
bar_chart1.add_data(bar_data1, titles_from_data=True)
|
|
|
|
|
bar_chart1.set_categories(bar_categories1)
|
|
|
|
|
# 调用函数并传入文件名和工作表名
|
|
|
|
|
|
|
|
|
|
# 添加柱状图1到工作表
|
|
|
|
|
ws.add_chart(bar_chart1, "V1")
|
|
|
|
|
|
|
|
|
|
# 创建柱状图
|
|
|
|
|
bar_chart = BarChart()
|
|
|
|
@ -176,17 +179,22 @@ if __name__ == '__main__':
|
|
|
|
|
# 保存Excel文件
|
|
|
|
|
wb.save('bilibili_videos_with_charts.xlsx')
|
|
|
|
|
|
|
|
|
|
def write_english_texts(file_path, sheet_name, texts):
|
|
|
|
|
# 打开 Excel 文件
|
|
|
|
|
wb = openpyxl.load_workbook(file_path)
|
|
|
|
|
sheet = wb[sheet_name]
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
create_bar_chart('bilibili_videos_with_charts.xlsx', 'Sheet')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 循环写入不同的英文文本到第一行的第17到21列
|
|
|
|
|
for i, text in enumerate(texts, start=17):
|
|
|
|
|
sheet.cell(row=1, column=i).value = text
|
|
|
|
|
|
|
|
|
|
# 保存文件
|
|
|
|
|
wb.save(file_path)
|
|
|
|
|
def write_english_texts(file_path, sheet_name, texts):
|
|
|
|
|
# 打开 Excel 文件
|
|
|
|
|
wb = openpyxl.load_workbook(file_path)
|
|
|
|
|
sheet = wb[sheet_name]
|
|
|
|
|
|
|
|
|
|
# 循环写入不同的英文文本到第一行的第17到21列
|
|
|
|
|
for i, text in enumerate(texts, start=17):
|
|
|
|
|
sheet.cell(row=1, column=i).value = text
|
|
|
|
|
|
|
|
|
|
# 保存文件
|
|
|
|
|
wb.save(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file_path = 'bilibili_videos_with_charts.xlsx' # Excel 文件路径
|
|
|
|
|