如何将Fountain格式剧本转换为适配自定义LaTeX模板的对应格式?
你可以直接用Python的fountain-parser包完成这个转换,它本身就是专门用来解析Fountain格式剧本的工具,会自动拆分舞台提示、角色、台词三类内容,不需要你自己写正则处理舞台提示位置不固定的问题。
使用步骤如下:
- 安装依赖包
pip install fountain-parser
- 使用示例转换代码:
from fountain import Fountain # 读取Fountain格式源文件 with open("input_script.fountain", "r", encoding="utf-8") as f: raw_content = f.read() # 解析剧本结构 parsed_script = Fountain(raw_content) latex_output = [] for item in parsed_script.elements: # 处理舞台提示,转成\textit格式 if item.element_type == "action": stage_direction = item.text.strip() if stage_direction: latex_output.append(f"\\textit{{{stage_direction}}}") # 处理对话,转成\dialog格式 elif item.element_type == "dialogue": # 把全大写的角色名转成首字母大写格式,和示例输出一致 formatted_char_name = item.character.name.title() dialogue_content = item.text.strip() latex_output.append(f"\\dialog{{{formatted_char_name}}}{{{dialogue_content}}}") # 写入输出LaTeX文件,空行分隔不同元素 with open("output_script.tex", "w", encoding="utf-8") as f: f.write("\n\n".join(latex_output))
如果你的剧本里还有场景标题、括号内角色提示等其他Fountain元素,只需要在遍历逻辑里增加对应的element_type判断,调整输出格式即可,不需要从零搭建解析逻辑。
内容的提问来源于stack exchange,提问作者Jannik Pitt
相关产品推荐
相关产品推荐

