feat(03_Story): 添加章节文件重命名工具并改进PDF合并排序逻辑
新增重命名脚本支持按规则批量修改章节文件名 改进merge_md_to_pdf.py中的文件排序逻辑,支持从文件名提取数字排序 添加工作区配置文件和更新后的PDF文档
This commit is contained in:
@@ -1,22 +1,37 @@
|
|||||||
"""
|
"""
|
||||||
将当前目录下的所有md文件合并成一个pdf文件
|
将当前目录下的所有md文件按文件名中的数字排序(从第一章开始),合并成一个pdf文件
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re # 新增:导入正则模块
|
||||||
import markdown
|
import markdown
|
||||||
from weasyprint import HTML, CSS
|
from weasyprint import HTML, CSS
|
||||||
|
|
||||||
|
def extract_chapter_number(filename):
|
||||||
|
"""
|
||||||
|
正则提取文件名中的数字(核心函数)
|
||||||
|
:param filename: 文件名(如:第1章.md、10.md、章节2.md)
|
||||||
|
:return: 提取到的数字(int),无数字返回9999(排最后)
|
||||||
|
"""
|
||||||
|
# 正则匹配所有连续数字(\d+ 匹配1个及以上数字)
|
||||||
|
match = re.search(r'\d+', filename)
|
||||||
|
if match:
|
||||||
|
return int(match.group()) # 转成整数,保证排序正确(10>2)
|
||||||
|
else:
|
||||||
|
return 9999 # 无数字的文件排最后
|
||||||
|
|
||||||
# 获取当前目录
|
# 获取当前目录
|
||||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
# 获取所有md文件并按文件名排序
|
# 获取所有md文件 + 按提取的数字排序(核心修正点)
|
||||||
md_files = sorted([f for f in os.listdir(current_dir) if f.endswith('.md')])
|
md_files = [f for f in os.listdir(current_dir) if f.endswith('.md')]
|
||||||
|
md_files = sorted(md_files, key=lambda x: extract_chapter_number(x)) # 按数字排序
|
||||||
|
|
||||||
print(f"找到 {len(md_files)} 个md文件:")
|
print(f"找到 {len(md_files)} 个md文件(按章节数字排序):")
|
||||||
for f in md_files:
|
for f in md_files:
|
||||||
print(f" - {f}")
|
print(f" - {f}")
|
||||||
|
|
||||||
# 合并所有md文件内容
|
# 合并所有md文件内容(原有逻辑不变)
|
||||||
combined_html = """<!DOCTYPE html>
|
combined_html = """<!DOCTYPE html>
|
||||||
<html lang=\"zh-CN\">
|
<html lang=\"zh-CN\">
|
||||||
<head>
|
<head>
|
||||||
@@ -69,7 +84,7 @@ for md_file in md_files:
|
|||||||
with open(file_path, 'r', encoding='utf-8') as f:
|
with open(file_path, 'r', encoding='utf-8') as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
# 转换为html
|
# 转换为html(保留表格、代码块格式)
|
||||||
md = markdown.Markdown(extensions=['tables', 'fenced_code'])
|
md = markdown.Markdown(extensions=['tables', 'fenced_code'])
|
||||||
html_content = md.convert(content)
|
html_content = md.convert(content)
|
||||||
|
|
||||||
|
|||||||
BIN
03_Story/西行代码(第2稿).pdf
Normal file
BIN
03_Story/西行代码(第2稿).pdf
Normal file
Binary file not shown.
BIN
03_Story/西行代码(第3稿).pdf
Normal file
BIN
03_Story/西行代码(第3稿).pdf
Normal file
Binary file not shown.
85
03_Story/重命名.py
Normal file
85
03_Story/重命名.py
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
def extract_chapter_number(filename):
|
||||||
|
"""
|
||||||
|
从文件名中提取章节号(支持"第15.5章"、"第16章"等格式)
|
||||||
|
返回:提取到的章节号字符串(如"15.5"、"16"),若未提取到返回None
|
||||||
|
"""
|
||||||
|
# 正则匹配"第X章",X可以是整数或小数(如15.5)
|
||||||
|
pattern = r"第(\d+(\.\d+)?)章"
|
||||||
|
match = re.search(pattern, filename)
|
||||||
|
if match:
|
||||||
|
return match.group(1)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def rename_chapter_files():
|
||||||
|
"""主函数:遍历当前目录文件并按规则重命名"""
|
||||||
|
# 获取当前脚本所在目录(也可手动指定路径,如 target_dir = r"D:\章节文件")
|
||||||
|
target_dir = os.getcwd()
|
||||||
|
# 遍历目录下所有文件(排除文件夹)
|
||||||
|
for filename in os.listdir(target_dir):
|
||||||
|
file_path = os.path.join(target_dir, filename)
|
||||||
|
if os.path.isdir(file_path): # 跳过文件夹
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 提取章节号
|
||||||
|
chapter_num = extract_chapter_number(filename)
|
||||||
|
if not chapter_num:
|
||||||
|
print(f"跳过:{filename} - 未识别到有效章节号(格式需包含'第X章')")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 计算新章节号
|
||||||
|
try:
|
||||||
|
new_chapter_num = None
|
||||||
|
if chapter_num == "15.5":
|
||||||
|
new_chapter_num = 16
|
||||||
|
else:
|
||||||
|
# 转为数字判断
|
||||||
|
num = float(chapter_num)
|
||||||
|
if num.is_integer(): # 确保是整数(如16→16,而非16.0)
|
||||||
|
num = int(num)
|
||||||
|
if num <= 15:
|
||||||
|
new_chapter_num = num
|
||||||
|
else:
|
||||||
|
new_chapter_num = num + 1
|
||||||
|
else:
|
||||||
|
print(f"跳过:{filename} - 章节号{chapter_num}不是整数/15.5(仅支持这两类)")
|
||||||
|
continue
|
||||||
|
except ValueError:
|
||||||
|
print(f"跳过:{filename} - 章节号{chapter_num}不是有效数字")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 构造新文件名(替换原章节号)
|
||||||
|
old_pattern = f"第{chapter_num}章"
|
||||||
|
new_pattern = f"第{new_chapter_num}章"
|
||||||
|
new_filename = filename.replace(old_pattern, new_pattern)
|
||||||
|
|
||||||
|
# 执行重命名(避免重名/文件相同的情况)
|
||||||
|
if new_filename == filename:
|
||||||
|
print(f"跳过:{filename} - 无需修改(章节号未变化)")
|
||||||
|
continue
|
||||||
|
|
||||||
|
old_path = os.path.join(target_dir, filename)
|
||||||
|
new_path = os.path.join(target_dir, new_filename)
|
||||||
|
|
||||||
|
# 检查新文件名是否已存在
|
||||||
|
if os.path.exists(new_path):
|
||||||
|
print(f"失败:{filename} - 新文件名{new_filename}已存在,无法重命名")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 执行重命名
|
||||||
|
try:
|
||||||
|
os.rename(old_path, new_path)
|
||||||
|
print(f"成功:{filename} → {new_filename}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"失败:{filename} - 重命名出错:{str(e)}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("===== 章节文件重命名工具 =====")
|
||||||
|
print("规则:15.5章→16章,16章及以后+1,1-15章不变")
|
||||||
|
print("-" * 30)
|
||||||
|
rename_chapter_files()
|
||||||
|
print("-" * 30)
|
||||||
|
print("重命名操作完成!按任意键退出...")
|
||||||
|
input() # 暂停窗口,方便查看结果
|
||||||
8
1stnovel.code-workspace
Normal file
8
1stnovel.code-workspace
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": ".."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user