实用脚本能批量拼接吗?

wen 实用脚本 10

本文目录导读:

实用脚本能批量拼接吗?

  1. 场景一:批量拼接文本文件(.txt, .csv, .log)
  2. 场景二:批量拼接视频(无转码,快速拼接)
  3. 场景三:批量拼接图片(长图或网格拼接)
  4. 场景四:批量拼接PDF文件
  5. 场景五:批量拼接Excel表格(按行或按列)
  6. 总结:你需要哪个?

这是一个很宽泛的问题,因为“拼接”可以指代很多不同的场景:视频拼接、图片拼接、PDF合并、文本文件合并、Excel表格合并等等。

答案是:当然可以,实用脚本”的精髓就在于批量处理(自动化)。

为了给你最准确的帮助,我整理了最常见的几种“批量拼接”场景及其对应的实用脚本(以Python和Shell为主,因为最常见且跨平台)。

场景一:批量拼接文本文件(.txt, .csv, .log)

这是最简单的,将一个文件夹里的所有文本按顺序合并成一个文件。

实用 Bash 脚本 (Linux/macOS/WSL):

#!/bin/bash
# 将所有 .txt 文件按文件名排序,合并到 output.txt
cat *.txt > output.txt
# 如果希望保留文件名作为分隔
for f in *.txt; do (cat "${f}"; echo "--- 文件名: $f ---") >> output_with_label.txt; done

实用 Python 脚本 (跨平台):

import os
output_file = "merged.txt"
folder_path = "."  # 当前目录,可以修改
with open(output_file, 'w', encoding='utf-8') as outfile:
    for filename in sorted(os.listdir(folder_path)):
        if filename.endswith(".txt"):
            filepath = os.path.join(folder_path, filename)
            with open(filepath, 'r', encoding='utf-8') as infile:
                outfile.write(infile.read())
                outfile.write("\n")  # 添加换行符分隔
print(f"合并完成: {output_file}")

场景二:批量拼接视频(无转码,快速拼接)

如果所有视频的编码、分辨率、帧率完全相同(例如同一台摄像机录制的MP4片段),可以使用无损拼接。

实用 FFmpeg 脚本 (推荐,命令行工具):

# 1. 先创建一个文件列表
for f in *.mp4; do echo "file '$f'" >> mylist.txt; done
# 2. 执行拼接 (注意文件名排序)
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4

优点:速度极快,画质无损。

场景三:批量拼接图片(长图或网格拼接)

将多张图片拼成一张长图,或者拼成2x2/3x3的网格。

实用 Python 脚本 (使用PIL/Pillow):

from PIL import Image
import os
image_folder = "./images/"
images = [Image.open(os.path.join(image_folder, f)) for f in sorted(os.listdir(image_folder)) if f.endswith(('.png','.jpg'))]
# 1. 垂直拼接 (长图)
widths, heights = zip(*(i.size for i in images))
total_height = sum(heights)
max_width = max(widths)
new_img = Image.new('RGB', (max_width, total_height))
y_offset = 0
for img in images:
    new_img.paste(img, (0, y_offset))
    y_offset += img.height
new_img.save('vertical_merged.jpg')
# 2. 网格拼接 (例如每行2张)
# (代码较长,可搜索“Pillow grid merge”)

需要安装: pip install Pillow

场景四:批量拼接PDF文件

合并多个PDF文件为一个。

实用 Python 脚本 (使用PyMuPDF或PyPDF2):

import PyPDF2
pdf_files = ["file1.pdf", "file2.pdf", "file3.pdf"] # 或使用os.listdir
merger = PyPDF2.PdfMerger()
for pdf in pdf_files:
    merger.append(pdf)
merger.write("merged_output.pdf")
merger.close()
print("PDF合并完成")

需要安装: pip install PyPDF2

场景五:批量拼接Excel表格(按行或按列)

合并多个Excel文件的行,或者将多个Sheet拼在一起。

实用 Python 脚本 (使用pandas):

import pandas as pd
import os
all_data = pd.DataFrame()
folder = './excel_files/'
for file in os.listdir(folder):
    if file.endswith('.xlsx'):
        df = pd.read_excel(os.path.join(folder, file))
        all_data = pd.concat([all_data, df], ignore_index=True) # 按行拼接
all_data.to_excel('all_merged.xlsx', index=False)
print("Excel合并完成")

需要安装: pip install pandas openpyxl


你需要哪个?

你要拼接什么? 推荐工具/方法 难度
文本/日志 cat (Linux) 或 Python
视频 (编码相同) ffmpeg (最有名) ⭐⭐
图片 (长图/网格) Python Pillow ⭐⭐
PDF Python PyPDF2 ⭐⭐
Excel Python pandas ⭐⭐

请告诉我你具体想拼接什么内容(视频、图片、文本、还是PDF?),我可以直接给你生成一个可以直接用的脚本。

抱歉,评论功能暂时关闭!