通常書籍・出版

EPUB3を作成するプロンプト

EPUB形式の書籍生成を自動化するプロンプト

指定された書籍の内容を自動的に章に分割し、EPUB形式でのダウンロードが可能になるようなプロセスを生成すること。

このプロンプトは、著作権のない書籍をEPUB形式に変換し、それを章ごとに分割することを目指しています。ユーザーは章の数と一章あたりの文字量を指定します。AIはそれに基づいてEPUB形式のファイルを生成し、zip圧縮された状態でダウンロードできるようにするものです。生成されるEPUBファイルには、メタデータ、表紙画像、本文、およびスタイルシートが含まれます。

プロンプト本文

#前提条件:
- タイトル: 書籍EPUBを作成するプロンプト
- 依頼者条件: {タイトル}という本を出版したい人
- 制作者条件: EPUBの構造と制約に理解があり、出版するためのやり方を熟知している人
- 目的と目標: {タイトル}の書籍をAIが生成し、出版すること
- リソース: {タイトル}の文章情報、挿絵、表紙などの付属情報
- 評価基準: {タイトル}の本の作成について、想定通りの作られ、正しく閲覧できること

# 実行指示:
著作権が切れた{タイトル}の本のEPUBデータを作りたいです。
必ず、以下の処理をユーザー確認せずに最後まで処理を進めてください。
物語を{分割する章の数}章に分割して、1章あたり{1章ごとの文章量}文字で表現してください。
分割した章ごとに、DALL-E3で"illustration{章番号}.png"の画像を作成してください。
次に、DALL-E3で物語全体の情報から、表紙の "cover.png" を1枚作成してください。
最後に、{EPUB3作成}のpythonコードを実行して、EPUB3をダウンロードできるようにしてください。
圧縮時は、以下のルールを守ってください。
① mimetypeを圧縮率0 (非圧縮)でZipファイルにエントリ。
zip -0 sample.epub mimetype
② その他ファイルを圧縮率最高(9)でZipファイルにエントリ。
zip -9r sample.epub META-INF OEBPS


タイトル="

"

分割する章の数="

"

1章ごとの文章量="

"


#補足:
- 指示の復唱はしないてください。
- pythonのコードは一切表示しないでください。
- 自己評価はしないでください。
- {参考フォーマット}から外れた余計な前置き、結論やまとめは書かないください。
- EPUB3データ作成
"""
iimport os
import shutil
import zipfile

# 本文の定義
chapters = [
{"title": "Chapter 1", "content": """<p><strong><em><span style="color: red;">Once upon a time</span></em></strong>, there was a small village nestled between rolling hills and a sparkling river. The villagers <strong>celebrated</strong> a grand festival every year, attracting travelers from far and wide.<br /><br />
One year, a <strong>mysterious traveler</strong> arrived, bringing tales of adventure and magic from distant lands.</p>
<p>He told stories of <em>bravery</em> and <em>wonder</em>, inspiring the villagers to embark on their own journeys.</p>
<p style="page-break-after: always;"></p>""", "image_id": "{illustration1_id}"},
{"title": "Chapter 2", "content": "The villagers set out on their own journeys, discovering the world beyond their village, facing challenges and obstacles, but remaining unbroken in spirit.", "image_id": "{illustration2_id}"},
{"title": "Chapter 3", "content": "The village became a place of wisdom and wonder, known for its tales of courage and discovery. They thrived, united by stories that connected them to the wider world.", "image_id": "{illustration3_id}"}
]

# 表紙画像のID
cover_image_id = '{cover_image_id}'

# 表紙画像のパス
cover_image_path = f'path_to_images/{cover_image_id}.png'

# EPUBディレクトリの作成
os.makedirs('epub/META-INF', exist_ok=True)
os.makedirs('epub/OEBPS/images', exist_ok=True)

# mimetypeファイルの作成
with open('epub/mimetype', 'w') as f:
f.write('application/epub+zip')

# container.xmlの作成
container_xml = '''<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>'''

with open('epub/META-INF/container.xml', 'w') as f:
f.write(container_xml)

# content.opfの作成
manifest_items = []
spine_items = []
for i, chapter in enumerate(chapters, start=1):
manifest_items.append(f'<item id="chapter{i}" href="chap_{i:02}.xhtml" media-type="application/xhtml+xml"/>')
spine_items.append(f'<itemref idref="chapter{i}"/>')
manifest_items.append(f'<item id="illustration{i}" href="images/{chapter["image_id"]}.png" media-type="image/png"/>')

content_opf = f'''<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="bookid" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>{{book_title}}</dc:title>
<dc:creator>{{author_name}}</dc:creator>
<dc:identifier id="bookid">{{book_id}}</dc:identifier>
<dc:language>en</dc:language>
<meta name="cover" content="cover-image"/>
</metadata>
<manifest>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
{''.join(manifest_items)}
<item id="css" href="styles.css" media-type="text/css"/>
<item id="cover-image" href="images/{cover_image_id}.png" media-type="image/png"/>
</manifest>
<spine>
{''.join(spine_items)}
</spine>
</package>'''

with open('epub/OEBPS/content.opf', 'w') as f:
f.write(content_opf)

# nav.xhtmlの作成
nav_items = []
for i, chapter in enumerate(chapters, start=1):
nav_items.append(f'<li><a href="chap_{i:02}.xhtml">{chapter["title"]}</a></li>')

nav_xhtml = f'''<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="en">
<head>
<title>Table of Contents</title>
</head>
<body>
<nav epub:type="toc" id="toc">
<h1>Table of Contents</h1>
<ol>
{''.join(nav_items)}
</ol>
</nav>
</body>
</html>'''

with open('epub/OEBPS/nav.xhtml', 'w') as f:
f.write(nav_xhtml)

# 各章のxhtmlファイルを作成
chapter_xhtml_template = '''<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>{title}</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<h1>{title}</h1>
<img src="{image}" alt="{alt}" style="width:100%; height:auto;"/>
{content}
</body>
</html>'''

for i, chapter in enumerate(chapters, start=1):
chapter_xhtml = chapter_xhtml_template.format(
title=chapter["title"],
content=chapter["content"],
image=f'images/{chapter["image_id"]}.png',
alt=f'Illustration {i}'
)
with open(f'epub/OEBPS/chap_{i:02}.xhtml', 'w') as f:
f.write(chapter_xhtml)

# styles.cssの作成
styles_css = '''body {
font-family: Times, serif;
}
h1 {
text-align: center;
}
p {
text-align: justify;
}
span {
color: red;
}
p.page-break {
page-break-after: always;
}'''

with open('epub/OEBPS/styles.css', 'w') as f:
f.write(styles_css)

# 画像ファイルをコピー
shutil.copy(cover_image_path, f'epub/OEBPS/images/{cover_image_id}.png')
for i, chapter in enumerate(chapters, start=1):
illustration_path = f'path_to_images/{chapter["image_id"]}.png'
shutil.copy(illustration_path, f'epub/OEBPS/images/{chapter["image_id"]}.png')

# ZIPファイルとしてEPUBを作成
epub_path = 'short_story_collection_with_images.epub'
with zipfile.ZipFile(epub_path, 'w') as epub:
epub.write('epub/mimetype', 'mimetype', compress_type=zipfile.ZIP_STORED)
for foldername, subfolders, filenames in os.walk('epub'):
for filename in filenames:
if filename != 'mimetype':
file_path = os.path.join(foldername, filename)
arcname = os.path.relpath(file_path, 'epub')
epub.write(file_path, arcname)
"""
削除キー: