好友
阅读权限10
听众
最后登录1970-1-1
|
最近在看考试的打印水印就烦人,顺便写一个python批量执行的
import os
import subprocess
# 定义PDF文件目录和命令路径
pdf_directory = r"大佬的文件和所有pdf在一个文件夹的路径"
pdf_commander_exe = os.path.join(pdf_directory, "PDFCommander.exe")
command_template = '{} -f "{}" -t "{}"'
# 定义要替换的字符串
replace_string = "需要替换的文本16进制字符串/根据自己需要自己替换"
# 遍历目录中的所有PDF文件并打印命令
pdf_files = [filename for filename in os.listdir(pdf_directory) if filename.endswith(".pdf")]
print("以下是需要执行的命令:")
for filename in pdf_files:
if '-new.pdf' in filename:
continue # 跳过已生成的新文件
command = command_template.format(pdf_commander_exe, filename, replace_string)
print(command)
# 执行命令,隐藏cmd窗口
subprocess.run(command, shell=True)
# 构建新文件名
new_filename = filename.replace('.pdf', '-new.pdf')
# 删除原始文件
original_file_path = os.path.join(pdf_directory, filename)
new_file_path = os.path.join(pdf_directory, new_filename)
if os.path.exists(new_file_path):
os.remove(original_file_path)
print("\n所有PDF文件的水印已删除并删除了原始文件。")
|
|