Certainly! Below is a Python script that accomplishes your requirements:
python
import os
import shutil
import subprocess
# Define the input directory containing PDF files
input_dir = r'D:\LLMs\MinerU\DataProcess\input_pdfs'
# Define the directory where the .md files will be stored
output_md_dir = r'D:\LLMs\MinerU\DataProcess\md_outputs'
# Define the directory where other files will be organized
other_files_base_dir = r'D:\LLMs\MinerU\DataProcess\other_outputs'
# Ensure the output directories exist
os.makedirs(output_md_dir, exist_ok=True)
os.makedirs(other_files_base_dir, exist_ok=True)
# Get a list of all PDF files in the input directory
pdf_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.pdf')]
for pdf_file in pdf_files:
input_path = os.path.join(input_dir, pdf_file)
pdf_basename = os.path.splitext(pdf_file)[0] # Filename without extension
# Create a temporary output directory for the current PDF file
temp_output_dir = os.path.join(input_dir, 'temp_output')
os.makedirs(temp_output_dir, exist_ok=True)
# Build and execute the magic-pdf command
command = [
'magic-pdf',
'-p', input_path,
'-o', temp_output_dir,
'-m', 'auto'
]
subprocess.run(command, check=True)
# Define the paths for the .md file and its destination
md_filename = f"{pdf_basename}.md"
md_file_path = os.path.join(temp_output_dir, md_filename)
md_output_path = os.path.join(output_md_dir, md_filename)
# Move the .md file to the md_outputs directory
if os.path.exists(md_file_path):
shutil.move(md_file_path, md_output_path)
else:
print(f"Warning: {md_filename} not found in {temp_output_dir}")
# Move other files to a subdirectory named after the input file
other_files_subdir = os.path.join(other_files_base_dir, pdf_basename)
os.makedirs(other_files_subdir, exist_ok=True)
for item in os.listdir(temp_output_dir):
# Skip the .md file (already moved)
if item == md_filename:
continue
src_item = os.path.join(temp_output_dir, item)
dst_item = os.path.join(other_files_subdir, item)
shutil.move(src_item, dst_item)
# Clean up the temporary output directory
os.rmdir(temp_output_dir)
print("Processing completed successfully.")
Instructions to use the script:
Set the Directories:
Update the input_dir variable with the path to the directory containing your input PDF files.
Update the output_md_dir variable with the path where you want to store the resulting .md files.
Update the other_files_base_dir variable with the path where you want to store the subdirectories containing other files.
Ensure Directories Exist:
The script uses os.makedirs() with exist_ok=True to create the output directories if they don't already exist.
Understanding the Script:
The script iterates over all .pdf files in the input_dir.
For each PDF file, it:
Executes the magic-pdf command to process the file.
Moves the resulting .md file to the output_md_dir.
Moves all other generated files into a subdirectory named after the PDF file (without extension) inside other_files_base_dir.
Cleans up by removing the temporary output directory used during processing.
Running the Script:
Make sure that the script has the necessary permissions to read and write to the specified directories.
Run the script in an environment where Python and the magic-pdf command are available.
Notes:
The script uses subprocess.run() to execute the magic-pdf command. The check=True parameter ensures that the script will raise an error if the command fails.
It assumes that the magic-pdf command generates output files in the directory specified by the -o option.
The script handles file paths in a way that should be compatible with Windows. Be sure to use raw strings (prefixing the strings with r) for Windows paths or escape backslashes properly.
If you need to process files in subdirectories or have additional requirements, you might need to modify the script accordingly.
Example Directory Structure After Processing:
D:\LLMs\MinerU\DataProcess\md_outputs
输入文件1.md
输入文件2.md
...
D:\LLMs\MinerU\DataProcess\other_outputs
输入文件1\ (contains other files generated from processing 输入文件1.pdf)
输入文件2\ (contains other files generated from processing 输入文件2.pdf)
...
Error Handling:
The script prints a warning if the expected .md file is not found in the temporary output directory.
You can add more robust error handling as needed, such as try-except blocks around file operations.
Dependencies:
Python standard libraries: os, shutil, subprocess.
Ensure that magic-pdf is installed and accessible from the command line where the script is executed.
This script should be accurate, concise, and easy to maintain. It automates the processing of multiple PDF files, manages the output files as per your requirements, and can be easily adjusted for future changes.