From 41750fe5ff7a3afb7c6f2e62577f1e7ae0e6f137 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 15 Mar 2015 12:01:20 +0530 Subject: [PATCH] Write out page sizes --- .../ebooks/conversion/plugins/docx_output.py | 13 +++++++++++++ src/calibre/ebooks/docx/writer/TODO | 2 +- src/calibre/ebooks/docx/writer/from_html.py | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/conversion/plugins/docx_output.py b/src/calibre/ebooks/conversion/plugins/docx_output.py index 915b56dd55..7de457afef 100644 --- a/src/calibre/ebooks/conversion/plugins/docx_output.py +++ b/src/calibre/ebooks/conversion/plugins/docx_output.py @@ -8,6 +8,9 @@ __copyright__ = '2013, Kovid Goyal ' 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): name = 'DOCX Output' @@ -15,6 +18,16 @@ class DOCXOutput(OutputFormatPlugin): file_type = 'docx' 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', help=_('Extract the contents of the generated %s file to the ' 'specified directory. The contents of the directory are first ' diff --git a/src/calibre/ebooks/docx/writer/TODO b/src/calibre/ebooks/docx/writer/TODO index 200e7ea794..844d2a6560 100644 --- a/src/calibre/ebooks/docx/writer/TODO +++ b/src/calibre/ebooks/docx/writer/TODO @@ -8,4 +8,4 @@ Lists Embed Fonts RTL text Lang support in run styles - +Create graphical config widget diff --git a/src/calibre/ebooks/docx/writer/from_html.py b/src/calibre/ebooks/docx/writer/from_html.py index 31ed9140f7..e576ca2a89 100644 --- a/src/calibre/ebooks/docx/writer/from_html.py +++ b/src/calibre/ebooks/docx/writer/from_html.py @@ -15,6 +15,7 @@ from calibre.ebooks.docx.names import namespaces 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.base import XPath, barename +from calibre.ebooks.pdf.render.common import PAPER_SIZES class Style(St): @@ -235,6 +236,18 @@ class Convert(object): doc.append(body) for block in self.blocks: 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'} E = ElementMaker(namespace=dn['w'], nsmap=dn)