-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtutorial_to_article.py
More file actions
executable file
·271 lines (191 loc) · 7.28 KB
/
tutorial_to_article.py
File metadata and controls
executable file
·271 lines (191 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python3
"""
基于教程素材生成公众号文章
将课程内容、案例、提示词框架转化为吸引人的公众号文章
"""
import os
import sys
from pathlib import Path
# 添加模块路径
sys.path.insert(0, os.path.dirname(__file__))
from modules.content_gen import ContentGenerator
from anthropic import Anthropic
class TutorialArticleGenerator:
"""教程文章生成器"""
def __init__(self):
self.gen = ContentGenerator()
self.client = Anthropic(api_key=self.gen.client.api_key)
def generate_from_tutorial(self, tutorial_content: str, article_type: str = "practical"):
"""
从教程内容生成公众号文章
Args:
tutorial_content: 教程原始内容
article_type: 文章类型
- practical: 实用教程型
- case_study: 案例分析型
- framework: 框架方法型
"""
print("\n" + "="*70)
print("📚 教程内容 → 公众号文章")
print("="*70 + "\n")
# 根据类型选择生成策略
if article_type == "practical":
prompt = self._build_practical_prompt(tutorial_content)
elif article_type == "case_study":
prompt = self._build_case_study_prompt(tutorial_content)
else:
prompt = self._build_framework_prompt(tutorial_content)
print("🔄 正在生成文章...")
print(f" 类型: {article_type}")
print(f" 风格: 北京大妞儿\n")
response = self.client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=8000,
temperature=0.7,
messages=[{"role": "user", "content": prompt}]
)
article = response.content[0].text
# 保存文章
output_dir = Path("output")
output_dir.mkdir(exist_ok=True)
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_file = output_dir / f"tutorial_article_{timestamp}.md"
with open(output_file, 'w', encoding='utf-8') as f:
f.write(article)
print("="*70)
print("✅ 文章生成完成!")
print("="*70)
print(f"📄 文件: {output_file}\n")
return str(output_file)
def _build_practical_prompt(self, tutorial_content: str) -> str:
"""构建实用教程型提示词"""
return f"""你是 ruby鑫燕,一位高级经管硕士背景的职场专家,同时也是个地道的北京大妞儿。
**你的任务**:
将以下教程内容改写成一篇吸引人的公众号文章。
**人设特点**:
- 幽默风趣,说话直接不绕弯子
- 简洁有力,从不说废话
- 平等对话,拒绝说教和爹味
- 用大白话解释专业概念
**语言风格**:
- 用"咱们""姐妹们""哥们儿"等亲切称呼
- 多用"其实""说白了""你想啊"等口语化表达
- 适当用北京话的语气词"呗""嘛""哈"
- 偶尔自嘲或吐槽,拉近距离
**写作要求**:
1. 标题要简洁有力(10-15字),制造冲突或好奇
2. 开头用具体场景+痛点,直接扎心
3. 把教程中的专业术语转化为大白话
4. 每个方法/框架都用"第一招""第二招"这样的说法
5. 加入个人使用经历或身边案例
6. 每段控制在3-5行,不要大段文字
7. 结尾有互动引导,但不说教
**务必避免**:
❌ 不要说"你应该""必须""要学会"等说教词汇
❌ 不要大段文字堆砌
❌ 不要过于学术化的表述
❌ 不要生硬的AI感
---
**教程原文**:
{tutorial_content}
---
**输出要求**(Markdown格式):
# 标题(简洁有力,10-15字)
开头段落(场景+痛点,3-5行)
## 第一招:XXX
内容...(加入个人经历或案例)
## 第二招:XXX
内容...
...
---
**写在最后**
结尾段落(互动引导,不说教)
现在开始改写!记住,要让读者觉得"这姐们儿说的真有意思",而不是"又在教我做事"。
"""
def _build_case_study_prompt(self, tutorial_content: str) -> str:
"""构建案例分析型提示词"""
return f"""你是 ruby鑫燕,一位高级经管硕士背景的职场专家,同时也是个地道的北京大妞儿。
**你的任务**:
将以下案例/调研内容改写成一篇深度分析文章。
**核心框架**:使用"真需求挖掘家"思维
1. **场景复现** - 用生动细节还原真实情况
2. **情绪触发点** - 找出用户痛点或爽点
3. **底层驱动** - 连到安全感、价值感、掌控感、成长感
4. **真需求结论** - 一句话总结,口语化、带温度
**语言风格**:
- 像讲故事一样娓娓道来
- 多用"你想啊""我观察到一个现象""这就有意思了"
- 加入个人洞察和评论
- 避免干巴巴的数据罗列
**写作要求**:
1. 标题要有悬念或反转
2. 开头直接抛出一个反常识的现象
3. 用案例中的具体细节渲染场景
4. 分析要有独特视角,不人云亦云
5. 结尾给出可操作的建议
---
**案例原文**:
{tutorial_content}
---
现在开始改写,要让读者看完觉得"原来是这么回事儿,长见识了!"
"""
def _build_framework_prompt(self, tutorial_content: str) -> str:
"""构建框架方法型提示词"""
return f"""你是 ruby鑫燕,一位高级经管硕士背景的职场专家,同时也是个地道的北京大妞儿。
**你的任务**:
将以下方法论/框架改写成易懂好用的实战指南。
**改写策略**:
1. 把抽象框架转化为具体场景
2. 每个步骤都给出实例
3. 用"普通做法 vs 高手做法"对比
4. 加入"我是这么用的"个人经验
**语言风格**:
- 把专业术语"翻译"成人话
- 多用类比和比喻
- "其实就是""说白了""你想啊"
- 加入吐槽和幽默
**文章结构**:
# 标题(简洁,带冲击)
开头:用失败案例引入("我见过太多人...")
## 这个方法到底是啥?
用大白话解释("说白了就是...")
## 怎么用?(分步骤)
### 第一步:XXX
- 具体怎么做
- 错误示范 vs 正确示范
- 我的实战经验
### 第二步:XXX
...
## 高手都这么玩
进阶技巧("我发现一个规律...")
---
**写在最后**
行动建议 + 互动引导
---
**框架原文**:
{tutorial_content}
---
现在开始改写!要让读者看完就能上手用,而不是"懂了很多道理,还是不会用"。
"""
def main():
"""主函数"""
import argparse
parser = argparse.ArgumentParser(description='教程内容转公众号文章')
parser.add_argument('tutorial_file', help='教程文件路径')
parser.add_argument('-t', '--type', default='practical',
choices=['practical', 'case_study', 'framework'],
help='文章类型')
args = parser.parse_args()
# 读取教程内容
with open(args.tutorial_file, 'r', encoding='utf-8') as f:
tutorial_content = f.read()
# 生成文章
generator = TutorialArticleGenerator()
output_file = generator.generate_from_tutorial(tutorial_content, args.type)
print("💡 下一步:")
print(f" 1. 查看文章: cat {output_file}")
print(f" 2. 转HTML: python md_to_html.py {output_file}")
print(f" 3. 审稿: python editor_review.py output/*_final.html\n")
if __name__ == "__main__":
main()