Python3锁定Excel表格M列第2行的方法
以下 Python3 代码示例展示如何锁定 Excel 表格的 M 列第 2 行。
import os
import re
import subprocess
import openpyxl
import math
from openpyxl.styles import Font, PatternFill, Alignment
# 视频格式
supported_formats = [
'.mp4', '.avi', '.mkv', '.wmv', '.mov', '.flv',
'.m2ts', '.ts', '.rm', '.rmvb', '.vob', '.3gp',
'.webm', '.hdmov', '.mp4v', '.mpv4', '.divx',
'.xvid', '.f4v', '.mpeg', '.asf', '.asx', '.m2t'
]
# 输出 Excel 路径
excel_folder = '.'
# 输出 Excel 文件名
excel_file = 'video_info.xlsx'
# 正则表达式匹配码率数据中的空格
pattern = re.compile(r'(\d+)\s+(\d+)')
# 创建 Excel 文件
wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'Video Info'
# 锁定表头
ws.freeze_panes = 'M2' # 修改为 'M2' 将 M 列第 2 行锁定
# 设置表头样式
header_font = Font(bold=True, color='800080') # 紫色
header_fill = PatternFill('solid', fgColor='C5E0B4')
header_alignment = Alignment(horizontal='center', vertical='center')
headers = [
'文件名',
'文件路径',
'格式',
'大小',
'时长',
'分辨率',
'码率',
'帧率',
'音频码率',
'音频采样率',
'音频语言',
'压缩比率',
'文件总数',
]
for col, header in enumerate(headers, start=1):
cell = ws.cell(row=1, column=col, value=header)
cell.font = header_font
cell.fill = header_fill
cell.alignment = header_alignment
# 处理失败文件个数
failed_count = 0
# 总计处理文件数
total_count = 0
# 遍历所有视频文件
success_count = 0
row = 2 # 从第二行开始写入数据
def process_video(video_file):
global success_count, row, failed_count
try:
# 获取文件名
file_name = os.path.basename(video_file)
# 获取文件大小
size_str = convert_size(os.path.getsize(video_file))
# 使用 mediainfo 获取视频信息 / 音频信息
with subprocess.Popen(['mediainfo',
'--Inform=Video;%Format%|%Width%x%Height%|%BitRate/String%|%FrameRate%|%Duration/String3%',
video_file],
stdout=subprocess.PIPE) as video_proc, \
subprocess.Popen(['mediainfo',
'--Inform=Audio;%BitRate/String%|%SamplingRate/String%|%Language/String%',
video_file],
stdout=subprocess.PIPE) as audio_proc:
video_output = video_proc.communicate()[0].decode().strip()
audio_output = audio_proc.communicate()[0].decode().strip()
# 解析输出结果
video_info = video_output.split('|')
audio_info = audio_output.split('|')
# 处理多音轨情况
audio_bitrate, audio_sampling_rate, audio_lang = zip(*[(audio_info[i*3],
audio_info[i*3+1], audio_info[i*3+2]) for i in range(len(audio_info)//3)])
# 时长取整
duration = video_info[4].split('.')[0]
h, m, s = duration.split(':')
duration = f'{h}:{m}:{str(s).zfill(2)}'
# 以分钟计算的时长
duration_minutes = int(h) * 60 + int(m) + math.ceil(float(s)) / 60
# 计算压缩比率
ratio = round(duration_minutes / os.path.getsize(video_file) * 1000000000, 2)
# 使用正则表达式替换码率数据中第 1、2 数字之间的空格
bitrate = re.sub(pattern, r'\1\2', video_info[2])
# 写入 Excel 文件
write_cell(ws, row, 1, file_name)
write_cell(ws, row, 2, os.path.dirname(video_file)) # 写入文件路径
write_cell(ws, row, 3, video_info[0])
write_cell(ws, row, 4, size_str)
write_cell(ws, row, 5, duration)
write_cell(ws, row, 6, video_info[1])
write_cell(ws, row, 7, bitrate)
write_cell(ws, row, 8, video_info[3])
write_cell(ws, row, 9, ' / '.join(audio_bitrate))
write_cell(ws, row, 10, ' / '.join(audio_sampling_rate))
write_cell(ws, row, 11, ' / '.join(audio_lang))
write_cell(ws, row, 12, ratio)
write_cell(ws, 2, 13, total_count) # 写入文件总数
# 打印进度
success_count += 1
row += 1
except Exception as e:
# 处理失败, 跳过并打印文件
failed_count += 1
print(f'处理文件 {video_file} 时失败:{e}')
# 写入失败到 err.txt
with open('err.txt', 'a') as f:
f.write(f'{video_file}\n')
return False
return True
def process_folder(video_folder):
global total_count
# 获取视频文件递归
video_files = []
for root, dirs, files in os.walk(video_folder):
for file in files:
if os.path.splitext(file)[1] in supported_formats:
video_files.append(os.path.join(root, file))
total_count = len(video_files)
if total_count == 0:
print('无视频文件或路径为空')
else:
for i, video_file in enumerate(video_files, start=1):
if process_video(video_file):
print(f'已处理 {success_count}/{total_count} 共计', end='\r')
else:
print(f'处理文件 {i}/{total_count} 时失败', end='\r')
print(f'已处理 {success_count}/{total_count} 共计')
def convert_size(size):
# 转换单位
units = ['B', 'KiB', 'MiB', 'GiB']
i = 0
while size >= 1024 and i < len(units)-1:
size /= 1024
i += 1
return f'{size:.2f} {units[i]}'
def write_cell(ws, row, col, value):
# 写入 Excel
cell = ws.cell(row=row, column=col, value=value)
if col == 13:
cell.font = Font(bold=True, color='FF0000')
if col in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
cell.alignment = Alignment(horizontal='left')
else:
cell.alignment = Alignment(horizontal='center')
if col not in [3]:
ws.column_dimensions[cell.column_letter].width = 11
# 文件路径
path = input('请输入文件路径:')
if os.path.isfile(path):
# 1个文件 total_count += 1
total_count = 1
process_video(path)
elif os.path.isdir(path):
process_folder(path)
if total_count > 0:
# 保存 Excel 文件
os.makedirs(excel_folder, exist_ok=True)
excel_file_path = os.path.join(excel_folder, excel_file)
wb.save(excel_file_path)
# 打印处理失败文件个数和总计处理文件数
print(
f'成功处理 {success_count} 个文件, 失败 {failed_count} 个文件, 视频信息已保存到 {excel_file_path}'
)
这段代码中,关键是 ws.freeze_panes = 'M2' 这行代码,它将 M 列第 2 行锁定,以便用户滚动浏览表格时,该行始终可见。
希望这个代码示例能帮助您锁定 Excel 表格的特定行和列。
注意: 以上代码使用 Python openpyxl 库来操作 Excel 文件。如果您的环境中尚未安装 openpyxl 库,可以使用以下命令安装:
pip install openpyxl
原文地址: https://www.cveoy.top/t/topic/mHf6 著作权归作者所有。请勿转载和采集!