import openpyxl from entity.BilibiliVideo import BilibiliVideo # 创建一个函数,用于将 BilibiliVideo 实例的属性写入 Excel 表格中 def write_to_excel(video_instances, excel_filename): wb = openpyxl.Workbook() ws = wb.active # 添加表头 ws.append(['bvId', 'title', 'url', 'uploadTime', 'uploadTimeText', 'topNo', 'viewCount', 'likeCount', 'coinCount', 'favoriteCount', 'commentCount', 'bulletCount', 'creatorId', 'creatorName', 'creatorFanCount']) # 遍历 BilibiliVideo 实例列表,逐个写入表格 for video_instance in video_instances: ws.append([video_instance.bvId, video_instance.title, video_instance.url, video_instance.uploadTime, video_instance.uploadTimeText, video_instance.topNo, video_instance.viewCount, video_instance.likeCount, video_instance.coinCount, video_instance.favoriteCount, video_instance.commentCount, video_instance.bulletCount, video_instance.creatorId, video_instance.creatorName, video_instance.creatorFanCount]) # 保存 Excel 文件 wb.save(excel_filename) if __name__ == '__main__': # 测试数据 test_cases = [ BilibiliVideo("BV1a4411C7i2", "视频标题1", "https://www.bilibili.com/video/BV1a4411C7i2", 1648927200, 1, 100000, 5000, 2000, 3000, 1000, 500, "up123456", "UP主A", 1000000), BilibiliVideo("BV1F5411S8h3", "视频标题2", "https://www.bilibili.com/video/BV1F5411S8h3", 1648830800, 2, 80000, 4000, 1500, 2000, 800, 400, "up234567", "UP主B", 800000), BilibiliVideo("BV1bW411d9c4", "视频标题3", "https://www.bilibili.com/video/BV1bW411d9c4", 1648734400, 3, 60000, 3000, 1000, 1500, 600, 300, "up345678", "UP主C", 600000), BilibiliVideo("BV1qy411z5k5", "视频标题4", "https://www.bilibili.com/video/BV1qy411z5k5", 1648638000, 4, 40000, 2000, 800, 1000, 400, 200, "up456789", "UP主D", 400000), BilibiliVideo("BV1R4411J0a6", "视频标题5", "https://www.bilibili.com/video/BV1R4411J0a6", 1648541600, 5, 20000, 1000, 500, 700, 200, 100, "up567890", "UP主E", 200000), BilibiliVideo("BV1gW411H8d7", "视频标题6", "https://www.bilibili.com/video/BV1gW411H8d7", 1648445200, 6, 10000, 500, 300, 400, 100, 50, "up678901", "UP主F", 100000), BilibiliVideo("BV1py411t6v8", "视频标题7", "https://www.bilibili.com/video/BV1py411t6v8", 1648348800, 7, 5000, 300, 200, 300, 50, 30, "up789012", "UP主G", 50000), BilibiliVideo("BV1Qy411S3x9", "视频标题8", "https://www.bilibili.com/video/BV1Qy411S3x9", 1648252400, 8, 2000, 100, 100, 200, 20, 10, "up890123", "UP主H", 20000), BilibiliVideo("BV1a4411C4y0", "视频标题9", "https://www.bilibili.com/video/BV1a4411C4y0", 1648156000, 9, 1000, 50, 50, 100, 10, 5, "up901234", "UP主I", 10000), BilibiliVideo("BV1F5411C2r1", "视频标题10", "https://www.bilibili.com/video/BV1F5411C2r1", 1648059600, 10, 500, 30, 20, 50, 5, 3, "up012345", "UP主J", 5000) ] # 将测试数据写入 Excel 文件 write_to_excel(test_cases, 'bilibili_videos.xlsx') import openpyxl def calculate_ratio_and_update(file_path, sheet_name): # 打开 Excel 文件 wb = openpyxl.load_workbook(file_path) sheet = wb[sheet_name] # 遍历除第一行外的每一行数据 for row_num in range(2, sheet.max_row + 1): value1 = sheet.cell(row=row_num, column=7).value #观看 value2 = sheet.cell(row=row_num, column=8).value#点赞 value3 = sheet.cell(row=row_num, column=9).value#投币 value4 = sheet.cell(row=row_num, column=10).value#收藏 value5 = sheet.cell(row=row_num, column=11).value#评论 value6 = sheet.cell(row=row_num, column=15).value#粉丝 value7 = sheet.cell(row=row_num, column=12).value # 弹幕 if value1 is not None and value2 is not None: value1 = float(value1) value2 = float(value2) value3 = float(value3) value4 = float(value4) value5 = float(value5) value6 = float(value6) ratio1 = value2 / value1 sheet.cell(row=row_num, column=17).value = ratio1 # ratio2 = value3 / value1 sheet.cell(row=row_num, column=18).value = ratio2 ratio3 = value4 / value1 sheet.cell(row=row_num, column=19).value = ratio3 ratio4 = value6 / value1 sheet.cell(row=row_num, column=20).value = ratio4 ratio5 = value5 + value7 sheet.cell(row=row_num, column=21).value = ratio5 # 保存文件 wb.save(file_path) if __name__ == '__main__': # 示例用法 file_path = 'bilibili_videos.xlsx' # Excel 文件路径 sheet_name = 'Sheet' # 工作表名称 calculate_ratio_and_update(file_path, sheet_name) print("Data analysis written to the Excel file.") import openpyxl from openpyxl.chart import ScatterChart, BarChart, AreaChart, Reference # 读取Excel文件 wb = openpyxl.load_workbook('bilibili_videos.xlsx') ws = wb.active # 创建柱状图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() bar_chart.title = "收益" bar_chart.y_axis.title = "Data" bar_chart.x_axis.title = "Index" # 设置柱状图数据 bar_data = Reference(ws, min_col=18, 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, "AD1") # 创建面积图 area_chart = AreaChart() area_chart.title = "视频实用性" area_chart.y_axis.title = "Data" area_chart.x_axis.title = "Index" # 设置面积图数据 area_data = Reference(ws, min_col=19, min_row=2, max_row=ws.max_row) area_categories = Reference(ws, min_col=2, min_row=2, max_row=ws.max_row) area_chart.add_data(area_data, titles_from_data=True) area_chart.set_categories(area_categories) # 添加面积图到工作表 ws.add_chart(area_chart, "AL1") # 创建柱状图 bar_chart_2 = BarChart() bar_chart_2.title = "视频惊艳程度" bar_chart_2.y_axis.title = "Data" bar_chart_2.x_axis.title = "Index" # 设置柱状图数据 bar_data_2 = Reference(ws, min_col=20, min_row=2, max_row=ws.max_row) bar_categories_2 = Reference(ws, min_col=2, min_row=2, max_row=ws.max_row) bar_chart_2.add_data(bar_data_2, titles_from_data=True) bar_chart_2.set_categories(bar_categories_2) # 添加柱状图到工作表 ws.add_chart(bar_chart_2, "AT1") # 创建柱状图 bar_chart_3 = BarChart() bar_chart_3.title = "视频的互动性" bar_chart_3.y_axis.title = "Data" bar_chart_3.x_axis.title = "Index" # 设置柱状图数据 bar_data_3 = Reference(ws, min_col=21, min_row=2, max_row=ws.max_row) bar_categories_3 = Reference(ws, min_col=2, min_row=2, max_row=ws.max_row) bar_chart_3.add_data(bar_data_3, titles_from_data=True) bar_chart_3.set_categories(bar_categories_3) # 添加柱状图到工作表 ws.add_chart(bar_chart_3, "BB1") # 保存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] # 循环写入不同的英文文本到第一行的第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 文件路径 sheet_name = 'Sheet' # 工作表名称 english_texts = [ "approve", "money", "practical", "Stunning", "interaction" ] # 要写入的英文文本列表 write_english_texts(file_path, sheet_name, english_texts) print("Data analysis chart written to the Excel file.")