5. 色々な変換

5.1. epub

sphinx-quickstartでepubの質問にyで答えます。

その後は、特に何も設定せず、make epubすればOKです。

make epub

もしyと答えていなくも、別のディレクトリでsphinx-quickstartを実行し、 epubをyで答えて生成されたconf.pyをコピーしてくればOKです。

すでにconf.pyを変更しているのであれば、epubをyで生成したconf.pyから:

# -- Options for Epub output ----------------------------------------------

という行から始まる設定行をコピーしてくればOKです。

5.2. pdf

5.2.1. latex経由

LaTexからpdfを作成する方法です。

こちらのほうがLaTexでレイアウトを編集してからPDFにできるので、 自由度は高いです。

しかし、私はLaTexを触ったことがないのでよくわかりません!

latex経由でPDFを作成するには、まずTexLive(win)かMacTex(Mac)をインストールします。

win:TeX Live - TeX Users Group
mac:MacTEX

MacTextをダウンロードするときは、mirror pageから日本のミラーからダウンロードした方がいいです。2.2Gもあるので・・・。

インストールできたら、make latexpdfjaを実行すればPDFが生成されます。

make latexpdfja

5.2.2. rst2pdf

rst2pdfというライブラリを使って、PDFを生成します。

こちらの方が高速に動作しますが、設定が大変で、 生成されるPDFもレイアウト調整できません。

pip install rst2pdf

MakefileにPDFのmake内容を追加します。

pdf:
    $(SPHINXBUILD) -b pdf $(ALLSPHINXOPTS) $(BUILDDIR)/pdf
    @echo "Build finished. The pdf files are in $(BUILDDIR)/pdf."

次の内容をconf.pyに追加して、設定内容を調整する。

extensions = ['rst2pdf.pdfbuilder']

#(省略)

# -- Options for PDF output --------------------------------------------------

# Grouping the document tree into PDF files. List of tuples
# (source start file, target name, title, author, options).
#
# If there is more than one author, separate them with \\.
# For example: r'Guido van Rossum\\Fred L. Drake, Jr., editor'
#
# The options element is a dictionary that lets you override
# this config per-document.
# For example,
# ('index', u'MyProject', u'My Project', u'Author Name',
#  dict(pdf_compressed = True))
# would mean that specific document would be compressed
# regardless of the global pdf_compressed setting.

pdf_documents = [
    ('PDFにするrstファイルの名前', u'ファイル名', u'ドキュメント名', u'作者名'),
]

# A comma-separated list of custom stylesheets. Example:
pdf_stylesheets = ['sphinx','kerning','a4','ja']

# Create a compressed PDF
# Use True/False or 1/0
# Example: compressed=True
#pdf_compressed = False
pdf_compressed = True

# A colon-separated list of folders to search for fonts. Example:
pdf_font_path = [os.getcwd()]

# Language to be used for hyphenation support
pdf_language = "ja"

# Mode for literal blocks wider than the frame. Can be
# overflow, shrink or truncate
#pdf_fit_mode = "shrink"
pdf_fit_mode = "shrink"

# Section level that forces a break page.
# For example: 1 means top-level sections start in a new page
# 0 means disabled
#pdf_break_level = 0
pdf_break_level = 1

# When a section starts in a new page, force it to be 'even', 'odd',
# or just use 'any'
pdf_breakside = 'any'

# Insert footnotes where they are defined instead of
# at the end.
pdf_inline_footnotes = True

# verbosity level. 0 1 or 2
#pdf_verbosity = 0

# If false, no index is generated.
#pdf_use_index = True

# If false, no modindex is generated.
#pdf_use_modindex = True

# If false, no coverpage is generated.
#pdf_use_coverpage = True

# Documents to append as an appendix to all manuals.
#pdf_appendices = []

# Enable experimental feature to split table cells. Use it
# if you get "DelayedTable too big" errors
#pdf_splittables = False

# Set the default DPI for images
#pdf_default_dpi = 72

次にja.jsonを追加します。

{
    "embeddedFonts" :
    [[
        "ipam.ttf",
        "ipag.ttf",
        "ipagp.ttf",
        "ipamp.ttf"
    ]],
    "fontsAlias" : {
        "stdFont": "IPAPMincho",
        "stdBold": "IPAPMincho",
        "stdItalic": "IPAPMincho",
        "stdBoldItalic": "IPAPMincho",
        "stdSans": "IPAPGothic",
        "stdSansBold": "IPAPGothic",
        "stdSansItalic": "IPAPGothic",
        "stdSansBoldItalic": "IPAPGothic",
        "stdMono": "IPAPGothic",
        "stdMonoItalic": "IPAPGothic",
        "stdMonoBold": "IPAPGothic",
        "stdMonoBoldItalic": "IPAPGothic",
        "stdSerif": "IPAPMincho"
    },
    "styles" : [
        ["base" , {
            "wordWrap": "CJK",
            "kerning" : true
        }],
        ["literal" , {
            "wordWrap": "None"
        }]
    ]
}

そして、最後にフォントを用意します。

日本語のフォントでTTFならよかったと思います。(他の形式は試したことない)

IPAフォントのダウンロード

4書体パックをダウンロードします。

  • ipam.ttf
  • ipag.ttf
  • ipagp.ttf
  • ipamp.ttf

ファイルをconf.pyと同じディレクトリに置きます。

これで準備完了です。

make pdf
open _build/pdf/filename.pdf

5.3. docx

sphinx拡張を用意しなくてはいけませんが、Wordファイル(docx)に変換できます。

pip install lxml
pip install PIL
mkdir exts
cd exts
hg clone https://bitbucket.org/haraisao/sphinx-docxbuilder

Makefileに以下を追加

docx:
    $(SPHINXBUILD) -b docx $(ALLSPHINXOPTS) $(BUILDDIR)/docx
    @echo
    @echo"Build finished. now you can process the docx file. $(BUILDDIR)/docx."

conf.pyに以下を追加

# -- Options for docx page output ---------------------------------------
sys.path.insert(0, os.path.abspath('exts'))
extensions += ['sphinx-docxbuilder']
Read the Docs v: latest
Versions
latest
Downloads
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.