一、安装依赖包
pip3 install xlrd
pip3 install pymysql
apt update
apt install ffmpeg
二、excel表格
三、脚本
#!/usr/bin/python3 # -*- coding:utf-8 -*- import xlrd import pymysql import os def condb(sql): ''' :param sql: 要执行的sql语句 :return: 一条记录和多条记录 ''' conn = pymysql.connect(host='x.x.x.x', user='root', password='123456', database='mysql', charset='utf8', port=3306) # 得到一个可以执行SQL语句并且将结果作为字典返回的游标(默认返回的结果为元组) local_cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) if 'SELECT' in sql: local_cursor.execute(sql) # 返回结果为字典格式,fetchone()返回一条记录 local_one = local_cursor.fetchone() if local_one: # 返回结果为字典格式,fetchall()返回多条记录 local_all = local_cursor.fetchall() local_all.insert(0, local_one) else: local_all = [] local_cursor.close() conn.close() return local_one, local_all elif 'UPDATE' or 'INSERT' in sql: try: local_one = local_cursor.execute(sql) local_all = None conn.commit() local_cursor.close() conn.close() return local_one, local_all except Exception as f: # Rollback in case there is any error print(f) conn.rollback() local_cursor.close() conn.close() # 指定excel文件 wb = xlrd.open_workbook('video.xls') # 指定使用哪个表 sh = wb.sheet_by_name('youtube') # 输出有效数据行数 # print(sh.nrows) # 输出有效数据列数 # print(sh.ncols) # 输出第一行第一列的值 # print(sh.cell(0,0).value) # 输出第一行的所有值 # print(sh.row_values(0)) # 将数据和标题组合成字典 # print(dict(zip(sh.row_values(0),sh.row_values(1)))) # 遍历excel,打印所有数据 for i in range(sh.nrows): if i > 0: print(str(i).split('.')[0] + ' ###################') # print(sh.row_values(i)) res = dict(zip(sh.row_values(0), sh.row_values(i))) condb( 'INSERT INTO `video` (title,youtube_id,category,tag,username,testurl,ctime,status,description) VALUES ("%s","%s","%s","%s","%s","%s",NOW(),1,"%s");' % ( res.get('title'), res.get('youtube_id'), res.get('category'), res.get('tag'), '李四', 'http://www.video.com/{}.mp4'.format(res.get('youtube_id')), '批量添加')) os.system( "ffmpeg -i '{}.mov' -s 1280*720 -b:v 1.5M zip/{}.mp4".format(str(i).split('.')[0], res.get('youtube_id')))