微调,增加了诗体总结和套语结尾。

This commit is contained in:
2026-01-07 13:39:38 +08:00
parent 66c2066dd7
commit c2eb39ff1e
8 changed files with 410 additions and 82 deletions

View File

@@ -0,0 +1,87 @@
"""
将当前目录下的所有md文件合并成一个pdf文件
"""
import os
import markdown
from weasyprint import HTML, CSS
# 获取当前目录
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取所有md文件并按文件名排序
md_files = sorted([f for f in os.listdir(current_dir) if f.endswith('.md')])
print(f"找到 {len(md_files)} 个md文件:")
for f in md_files:
print(f" - {f}")
# 合并所有md文件内容
combined_html = """<!DOCTYPE html>
<html lang=\"zh-CN\">
<head>
<meta charset=\"UTF-8\">
<title>小说合集</title>
<style>
@page {
margin: 2cm;
@bottom-right {
content: counter(page);
}
}
body {
font-family: \"SimSun\", \"宋体\", serif;
font-size: 12pt;
line-height: 1.8;
text-align: justify;
}
h1 {
font-size: 18pt;
text-align: center;
margin-top: 2em;
margin-bottom: 1em;
}
h2 {
font-size: 14pt;
text-align: center;
margin-top: 1.5em;
margin-bottom: 0.8em;
}
p {
margin: 0.5em 0;
text-indent: 2em;
}
.chapter-title {
page-break-before: always;
}
.chapter-title:first-child {
page-break-before: auto;
}
</style>
</head>
<body>
"""
for md_file in md_files:
file_path = os.path.join(current_dir, md_file)
# 读取md文件
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 转换为html
md = markdown.Markdown(extensions=['tables', 'fenced_code'])
html_content = md.convert(content)
# 添加章节标题
chapter_title = md_file.replace('.md', '')
combined_html += f'<h1 class=\"chapter-title\">{chapter_title}</h1>\n'
combined_html += html_content + '\n'
combined_html += '</body></html>'
# 输出pdf文件
output_pdf = os.path.join(current_dir, '小说合集.pdf')
HTML(string=combined_html).write_pdf(output_pdf)
print(f"\nPDF已生成: {output_pdf}")

Binary file not shown.

Binary file not shown.

Binary file not shown.