duyunzhoudyz/dyz-gongwen-skills / skills/gongwen-format
gongwen-format
Verified按 GB/T 9704-2012 把公文正文排成 Word,有红头、信函、纪要、命令(令)、白头、材料、简报七种版式,支持联合行文版头、附件另面、预印红头纸套打;检查、修复 docx 的页边距、字体、行距、页码;把核稿意见写成 Word 批注;无 Python 时给手动设置清单。用户说“排版”“套红头”“转成 Word”“按国标调格式”“格式乱了帮我改”“页码怎么设”时使用。只管格式不改内容:起草转对应起草 skill,压缩改写转 gongwen-polish,去 AI 味转 gongwen-humanize,去免责对冲转 gongwen-direct,核稿转 gongwen-review。
scripts/gw_verify.py
View on GitHub# -*- coding: utf-8 -*-
"""文字核对\uff1a输入全文与文档全文规范化后双向\u3001按顺序逐字比对\uff08只依赖标准库\uff09\u3002
多字\uff08文档里有\u3001输入里没有\uff09\u3001少字\uff08输入里有\u3001文档里没有\uff09\u3001乱序\uff08位置不一致\uff09\u3001重复\uff08同一段文字排了两次\uff09都算不一致\u3002
build_docx.py --keep-text 保存前用它核对\uff0c不一致就中止\uff1b抽取\uff08extract_docx.py\uff09后用它核对\u201c版式信息 + 正文\u201d是否等于原文\u3002
规范化\uff1a去掉全部空白\uff08含全角空格\u3001制表符\u3001换行\uff09\u3002核对通过前\uff0c不得对用户声称\u201c已逐字核对\u201d\u3002
"""
from __future__ import annotations
import difflib
import re
from dataclasses import dataclass, field
from typing import List, Sequence
# 文档里由版式信息写出的固定标签字,核对“多出”的文字时不计(如“签发人:”“抄送:”“某日印发”“附件1”)
LABEL_WORDS = ("签发人\uff1a", "抄送\uff1a", "附件\uff1a", "总第", "★", "印发", "文件", "附件", "第", "号", "期", "编")
def norm(text: str) -> str:
"""规范化\uff1a去掉全部空白字符\u3002"""
return re.sub(r"\s+", "", text or "")
@dataclass
class TextCheck:
ok: bool = True
chars: int = 0
problems: List[str] = field(default_factory=list)
def summary(self, limit: int = 5) -> str:
return "\uff1b".join(self.problems[:limit]) + (f";……共 {len(self.problems)} 处" if len(self.problems) > limit else "")
def _pieces(a: str, b: str):
"""a\uff08输入\uff09与 b\uff08文档\uff09的字符级差异\uff1a返回 (少的文字列表, 多的文字列表)\u3002"""
missing: List[str] = []
extra: List[str] = []
for op, i1, i2, j1, j2 in difflib.SequenceMatcher(None, a, b, autojunk=False).get_opcodes():
if op in ("delete", "replace") and a[i1:i2]:
missing.append(a[i1:i2])
if op in ("insert", "replace") and b[j1:j2]:
extra.append(b[j1:j2])
return missing, extra
def _covered(piece: str, values: Sequence[str]) -> bool:
"""piece 是否能完全由版式信息的值和固定标签字依次拼成\u3002"""
words = sorted({v for v in list(values) + list(LABEL_WORDS) if v}, key=len, reverse=True)
reach = [False] * (len(piece) + 1)
reach[0] = True
for i in range(len(piece)):
if not reach[i]:
continue
for w in words:
if piece.startswith(w, i):
reach[i + len(w)] = True
return reach[len(piece)]
def compare_text(doc_texts: Sequence[str], input_texts: Sequence[str], *, allowed: Sequence[str] = (),
movable: Sequence[str] = ()) -> TextCheck:
"""比对文档全文与输入全文\u3002
doc_texts\uff1a文档各段落文字\uff08按文档顺序\uff0c含表格单元格\uff09\uff1binput_texts\uff1a输入各行\u3001表格单元格文字\uff08按输入顺序\uff09\u3002
allowed\uff1a版式信息中提供的值\uff08文档里由它们写出的文字不算\u201c多出\u201d\uff0c其他多出的文字报错\uff09\u3002
movable\uff1a允许换位置的文字\uff08版头要素\u3001版记行\uff0c排版时按版式移到版头\u3001版记位置\uff09\u3002
"""
inp = [t for t in (norm(x) for x in input_texts) if t]
doc = [t for t in (norm(x) for x in doc_texts) if t]
allow = [a for a in (norm(x) for x in allowed) if a]
mov = {norm(x) for x in movable if norm(x)}
res = TextCheck(chars=sum(len(t) for t in inp))
missing: List[str] = []
extra: List[str] = []
for op, i1, i2, j1, j2 in difflib.SequenceMatcher(None, inp, doc, autojunk=False).get_opcodes():
if op == "equal":
continue
ri, rd = "".join(inp[i1:i2]), "".join(doc[j1:j2])
if ri == rd:
continue # 只是分段不同(如标题分行),文字一致
m, e = _pieces(ri, rd)
missing += m
extra += e
# 文字相同、位置不同:版头、版记要素按版式换位置,允许;其他是乱序
for text in list(missing):
if text in extra:
missing.remove(text)
extra.remove(text)
if text not in mov:
res.problems.append(f"乱序:“{text[:30]}”在文档中的位置与输入不一致")
for text in missing:
res.problems.append(f"少字:输入中的“{text[:30]}”文档里没有")
inp_set = set(inp)
for text in extra:
if text in inp_set:
res.problems.append(f"重复:“{text[:30]}”在文档中多排了一次")
elif not _covered(text, allow):
res.problems.append(f"多字:文档里多出输入中没有的“{text[:30]}”")
res.ok = not res.problems
return res
def compare_chars(original_texts: Sequence[str], rebuilt_texts: Sequence[str], *, ignore: Sequence[str] = ()) -> TextCheck:
"""按字统计核对\uff08不看顺序\uff09\uff1a原文全文与\u201c版式信息加正文\u201d重组后的全文\uff0c各字出现次数应一致\uff0c多了或少了都报出\u3002
抽取\uff08extract_docx.py\uff09用\uff1a版式信息里的要素已从正文移出\uff0c顺序与原文不同\uff0c所以只核对文字有没有多\u3001少\u3001重复\uff1b
ignore\uff1a两边都去掉的固定标签字和记号\uff08如\u201c签发人\uff1a\u201d\u201c抄送\uff1a\u201d\u201c【\u201d\u201c】\u201d\uff09\u3002"""
from collections import Counter
def flat(texts: Sequence[str]) -> str:
out = norm("".join(texts))
for word in ignore:
out = out.replace(word, "")
return out
a, b = Counter(flat(original_texts)), Counter(flat(rebuilt_texts))
res = TextCheck(chars=sum(a.values()))
lost, gained = a - b, b - a
if lost:
res.problems.append("少字\uff1a原文有\u3001版式信息和正文里没有的字\uff1a" + "".join(f"{c}×{n} " for c, n in lost.most_common(12)).strip())
if gained:
res.problems.append("多字或重复\uff1a版式信息和正文里多出的字\uff1a" + "".join(f"{c}×{n} " for c, n in gained.most_common(12)).strip())
res.ok = not res.problems
return res
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 |