Write out page sizes

This commit is contained in:
Kovid Goyal 2015-03-15 12:01:20 +05:30
parent d238cc4a6f
commit 41750fe5ff
3 changed files with 27 additions and 1 deletions

View File

@ -8,6 +8,9 @@ __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
PAGE_SIZES = ['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'b0', 'b1',
'b2', 'b3', 'b4', 'b5', 'b6', 'legal', 'letter']
class DOCXOutput(OutputFormatPlugin): class DOCXOutput(OutputFormatPlugin):
name = 'DOCX Output' name = 'DOCX Output'
@ -15,6 +18,16 @@ class DOCXOutput(OutputFormatPlugin):
file_type = 'docx' file_type = 'docx'
options = { options = {
OptionRecommendation(name='docx_page_size', recommended_value='letter',
level=OptionRecommendation.LOW, choices=PAGE_SIZES,
help=_('The size of the page. Default is letter. Choices '
'are %s') % PAGE_SIZES),
OptionRecommendation(name='docx_custom_page_size', recommended_value=None,
help=_('Custom size of the document. Use the form widthxheight '
'EG. `123x321` to specify the width and height (in pts). '
'This overrides any specified page-size.')),
OptionRecommendation(name='extract_to', OptionRecommendation(name='extract_to',
help=_('Extract the contents of the generated %s file to the ' help=_('Extract the contents of the generated %s file to the '
'specified directory. The contents of the directory are first ' 'specified directory. The contents of the directory are first '

View File

@ -8,4 +8,4 @@ Lists
Embed Fonts Embed Fonts
RTL text RTL text
Lang support in run styles <w:lang> Lang support in run styles <w:lang>
Create graphical config widget

View File

@ -15,6 +15,7 @@ from calibre.ebooks.docx.names import namespaces
from calibre.ebooks.docx.writer.styles import w, StylesManager from calibre.ebooks.docx.writer.styles import w, StylesManager
from calibre.ebooks.oeb.stylizer import Stylizer as Sz, Style as St from calibre.ebooks.oeb.stylizer import Stylizer as Sz, Style as St
from calibre.ebooks.oeb.base import XPath, barename from calibre.ebooks.oeb.base import XPath, barename
from calibre.ebooks.pdf.render.common import PAPER_SIZES
class Style(St): class Style(St):
@ -235,6 +236,18 @@ class Convert(object):
doc.append(body) doc.append(body)
for block in self.blocks: for block in self.blocks:
block.serialize(body) block.serialize(body)
width, height = PAPER_SIZES[self.opts.docx_page_size]
if self.opts.docx_custom_page_size is not None:
width, height = map(float, self.opts.docx_custom_page_size.partition('x')[0::2])
width, height = int(20 * width), int(20 * height)
def margin(which):
return w(which), str(int(getattr(self.opts, 'margin_'+which) * 20))
body.append(E.sectPr(
E.pgSz(**{w('w'):str(width), w('h'):str(height)}),
E.pgMar(**dict(map(margin, 'left top right bottom'.split()))),
E.cols(**{w('space'):'720'}),
E.docGrid(**{w('linePitch'):"360"}),
))
dn = {k:v for k, v in namespaces.iteritems() if k in 'wr'} dn = {k:v for k, v in namespaces.iteritems() if k in 'wr'}
E = ElementMaker(namespace=dn['w'], nsmap=dn) E = ElementMaker(namespace=dn['w'], nsmap=dn)