tance-mang/chinese-webnovel-skills / skills/draft
draft
Verified把小说写成"供修改的 Word 草稿"。当用户要把稿子导成 Word/docx、要分章节带目录、章标题左对齐、要标出可以改的地方让自己修改、或说"先写个大概我来筛选修改"时使用。产出便于作者修改和复制的 Word 草稿,章标题左对齐,修改建议默认独立保存,命名《书名》修改N,绝不覆盖原文。
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.0 KB |
| build_docx.py | script | 5.8 KB |
build_docx.py
View on GitHub# -*- coding: utf-8 -*-
"""
把 Markdown 草稿转成"供修改的 Word 草稿"。
用法:
python build_docx.py 输入.md --title "书名" [--outdir 目录]
Markdown 约定:
# 第一章 章名 -> 章标题(左对齐,进目录)
正文段落...
【修改:建议……】 -> 红色高亮的修改建议(行内或独立成段都可)
输出:《书名》修改N.docx(N 自动递增,绝不覆盖原文或已有"修改"文件),含目录。
"""
import sys, os, re, argparse
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_COLOR_INDEX
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
SUGGEST_RE = re.compile(r'【修改[::][^】]*】')
CHAPTER_RE = re.compile(r'^\s*#*\s*(第[0-9零一二三四五六七八九十百千两]+章.*)$')
def set_cjk(run, font='SimSun'):
run.font.name = font
rpr = run._element.get_or_add_rPr()
rpr.rFonts.set(qn('w:eastAsia'), font)
def set_first_line_indent_2chars(paragraph):
"""首行缩进 2 字符(Word 标准中文排版)。
用 firstLineChars=200(按字符,跟字号走),并给 firstLine 兜底。"""
pPr = paragraph._p.get_or_add_pPr()
ind = pPr.find(qn('w:ind'))
if ind is None:
ind = OxmlElement('w:ind')
pPr.append(ind)
ind.set(qn('w:firstLineChars'), '200') # 200 = 2 字符
ind.set(qn('w:firstLine'), '480') # 兜底:480 twips ≈ 2 字(12pt)
def add_title(doc, title):
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
r = p.add_run(title)
r.bold = True
r.font.size = Pt(22)
r.font.color.rgb = RGBColor(0, 0, 0) # 纯黑,不要默认颜色
set_cjk(r, 'SimHei')
def add_toc(doc):
doc.add_paragraph().add_run('目录').bold = True
p = doc.add_paragraph()
run = p.add_run()
for ftype, text in [('begin', None), (None, 'TOC \\o "1-3" \\h \\z \\u'), ('separate', None)]:
pass
fld_begin = OxmlElement('w:fldChar'); fld_begin.set(qn('w:fldCharType'), 'begin')
instr = OxmlElement('w:instrText'); instr.set(qn('xml:space'), 'preserve'); instr.text = 'TOC \\o "1-3" \\h \\z \\u'
fld_sep = OxmlElement('w:fldChar'); fld_sep.set(qn('w:fldCharType'), 'separate')
tnode = OxmlElement('w:t'); tnode.text = '(在 Word 里右键此处 → 更新域,即可生成页码目录)'
fld_end = OxmlElement('w:fldChar'); fld_end.set(qn('w:fldCharType'), 'end')
for node in (fld_begin, instr, fld_sep, tnode, fld_end):
run._r.append(node)
doc.add_page_break()
def add_chapter(doc, text):
p = doc.add_heading(level=1)
p.alignment = WD_ALIGN_PARAGRAPH.LEFT # 第X章 左对齐
r = p.add_run(text)
r.font.color.rgb = RGBColor(0, 0, 0) # 章标题纯黑,不要 Word 默认的蓝色
set_cjk(r, 'SimHei')
def add_body(doc, line):
"""正文段落;其中【修改:…】渲染成红色高亮。"""
p = doc.add_paragraph()
set_first_line_indent_2chars(p) # 每段首行缩进 2 字符
pos = 0
for m in SUGGEST_RE.finditer(line):
if m.start() > pos:
r = p.add_run(line[pos:m.start()]); set_cjk(r)
r = p.add_run(m.group(0))
r.font.color.rgb = RGBColor(0xC0, 0x00, 0x00)
r.font.highlight_color = WD_COLOR_INDEX.YELLOW
r.bold = True
set_cjk(r)
pos = m.end()
if pos < len(line):
r = p.add_run(line[pos:]); set_cjk(r)
def pick_filename(title, outdir):
"""《书名》修改N.docx,N 从 1 起自增,绝不覆盖原文或已有修改稿。"""
n = 1
while True:
name = f'《{title}》修改{n}.docx'
path = os.path.join(outdir, name)
if not os.path.exists(path):
return path
n += 1
def main():
ap = argparse.ArgumentParser()
ap.add_argument('input')
ap.add_argument('--title', required=True)
ap.add_argument('--outdir', default='.')
ap.add_argument('--inline-suggestions', action='store_true', help='保留正文中的修改标记;默认单独导出建议')
args = ap.parse_args()
with open(args.input, encoding='utf-8') as f:
lines = f.read().splitlines()
doc = Document()
style = doc.styles['Normal']; style.font.name = 'SimSun'; style.font.size = Pt(12)
style._element.rPr.rFonts.set(qn('w:eastAsia'), 'SimSun')
add_title(doc, args.title)
add_toc(doc)
body_chars = 0
clean_lines = []
suggestions = []
chapter = '未分章'
for line in lines:
if not line.strip():
continue
for suggestion in SUGGEST_RE.findall(line):
suggestions.append(f'{chapter} | {SUGGEST_RE.sub("", line)[:40]} | {suggestion}')
clean = SUGGEST_RE.sub('', line).strip()
if not args.inline_suggestions:
line = clean
if not line.strip():
continue
cm = CHAPTER_RE.match(line)
if cm:
chapter = cm.group(1).strip()
add_chapter(doc, chapter)
clean_lines.append(chapter)
else:
add_body(doc, line)
if clean:
clean_lines.append(clean)
body_chars += len(SUGGEST_RE.sub('', line))
os.makedirs(args.outdir, exist_ok=True)
out = pick_filename(args.title, args.outdir)
doc.save(out)
stem = os.path.splitext(out)[0]
with open(stem + '.txt', 'w', encoding='utf-8') as f:
f.write('\n\n'.join(clean_lines) + '\n')
if suggestions:
with open(stem + '_修改建议.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(suggestions) + '\n')
# 字数提示(不含修改建议)
tip = '' # 导出不根据短篇门槛要求单章扩写
print(f'[OK] 已生成:{out}')
print(f' 正文约 {body_chars} 字{tip}')
print(f' 目录已插入(Word 中更新域);已附纯文本复制稿,建议默认独立保存')
if __name__ == '__main__':
main()
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| canvas-designanthropics/skillsCreate beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| docxanthropics/skillsUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| pdfanthropics/skillsUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| pptxanthropics/skillsUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an email or summary); editing, modifying, or updating existing presentations; combining or splitting slide files; working with templates (.potx), layouts, speaker notes, or comments. Trigger whenever the user mentions "deck," "slides," "presentation," or references a .pptx or .potx filename, regardless of what they plan to do with the content afterward. If a .pptx or .potx file needs to be opened, created, or touched, use this skill. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |