DOCX Output: Add separate, docx specific, page margin settings that override the common settings

This commit is contained in:
Kovid Goyal 2017-04-13 08:12:41 +05:30
parent ed8688ee7f
commit 98f4712f0d
4 changed files with 52 additions and 78 deletions

View File

@ -40,13 +40,31 @@ class DOCXOutput(OutputFormatPlugin):
help=_('Extract the contents of the generated %s file to the '
'specified directory. The contents of the directory are first '
'deleted, so be careful.') % 'DOCX'),
}
recommendations = {
('margin_left', 72.0, OptionRecommendation.MED),
('margin_right', 72.0, OptionRecommendation.MED),
('margin_top', 72.0, OptionRecommendation.MED),
('margin_bottom', 72.0, OptionRecommendation.MED),
OptionRecommendation(name='docx_page_margin_left', recommended_value=72.0,
level=OptionRecommendation.LOW,
help=_('The size of the left page margin, in pts. Default is 72pt.'
' Overrides the main left page margin setting.')
),
OptionRecommendation(name='docx_page_margin_top', recommended_value=72.0,
level=OptionRecommendation.LOW,
help=_('The size of the top page margin, in pts. Default is 72pt.'
' Overrides the main top page margin setting, unless set to zero.')
),
OptionRecommendation(name='docx_page_margin_right', recommended_value=72.0,
level=OptionRecommendation.LOW,
help=_('The size of the right page margin, in pts. Default is 72pt.'
' Overrides the main right page margin setting, unless set to zero.')
),
OptionRecommendation(name='docx_page_margin_bottom', recommended_value=72.0,
level=OptionRecommendation.LOW,
help=_('The size of the bottom page margin, in pts. Default is 72pt.'
' Overrides the main bottom page margin setting, unless set to zero.')
),
}
def convert_metadata(self, oeb):
@ -68,4 +86,3 @@ class DOCXOutput(OutputFormatPlugin):
if opts.extract_to:
from calibre.ebooks.docx.dump import do_dump
do_dump(output_path, opts.extract_to)

View File

@ -50,7 +50,10 @@ def create_skeleton(opts, namespaces=None):
width, height = int(20 * width), int(20 * height)
def margin(which):
return w(which), str(int(getattr(opts, 'margin_'+which) * 20))
val = getattr(opts, 'docx_page_margin_' + which)
if val == 0.0:
val = getattr(opts, 'margin_' + which)
return w(which), str(int(val * 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()))),
@ -260,6 +263,7 @@ class DOCX(object):
for fname, data in self.fonts.iteritems():
zf.writestr(fname, data)
if __name__ == '__main__':
d = DOCX(None, None)
print (d.websettings)

View File

@ -4,14 +4,15 @@ __license__ = 'GPL 3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'
from calibre.gui2.convert.docx_output_ui import Ui_Form
from PyQt5.Qt import QFormLayout, QComboBox, QCheckBox, QLineEdit, QDoubleSpinBox, QSizePolicy
from calibre.gui2.convert import Widget
paper_size_model = None
orientation_model = None
class PluginWidget(Widget, Ui_Form):
class PluginWidget(Widget):
TITLE = _('DOCX Output')
HELP = _('Options specific to')+' DOCX '+_('output')
@ -21,9 +22,29 @@ class PluginWidget(Widget, Ui_Form):
def __init__(self, parent, get_option, get_help, db=None, book_id=None):
Widget.__init__(self, parent, [
'docx_page_size', 'docx_custom_page_size', 'docx_no_cover', 'docx_no_toc',
'docx_page_margin_left', 'docx_page_margin_top', 'docx_page_margin_right',
'docx_page_margin_bottom',
])
for x in get_option('docx_page_size').option.choices:
self.opt_docx_page_size.addItem(x)
self.initialize_options(get_option, get_help, db, book_id)
self.layout().setFieldGrowthPolicy(self.layout().ExpandingFieldsGrow)
def setupUi(self, *a):
self.l = l = QFormLayout(self)
self.opt_docx_page_size = QComboBox(self)
l.addRow(_('Paper si&ze:'), self.opt_docx_page_size)
self.opt_docx_custom_page_size = w = QLineEdit(self)
w.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
l.addRow(_('&Custom size:'), w)
for i, text in enumerate((_('Page &left margin'), _('Page &top margin'), _('Page &right margin'), _('Page &bottom margin'))):
m = 'left top right bottom'.split()[i]
w = QDoubleSpinBox(self)
w.setRange(-100, 500), w.setSuffix(' pt'), w.setDecimals(1)
setattr(self, 'opt_docx_page_margin_' + m, w)
l.addRow(text + ':', w)
self.opt_docx_no_toc = QCheckBox(_('Do not insert the &Table of Contents as a page at the start of the document'))
l.addRow(self.opt_docx_no_toc)
self.opt_docx_no_cover = QCheckBox(_('Do not insert &cover as image at start of document'))
l.addRow(self.opt_docx_no_cover)

View File

@ -1,68 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>638</width>
<height>588</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Paper Si&amp;ze:</string>
</property>
<property name="buddy">
<cstring>opt_docx_page_size</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="opt_docx_page_size"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&amp;Custom size:</string>
</property>
<property name="buddy">
<cstring>opt_docx_custom_page_size</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="opt_docx_custom_page_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QCheckBox" name="opt_docx_no_cover">
<property name="text">
<string>Do not insert &amp;cover as image at start of document</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="opt_docx_no_toc">
<property name="text">
<string>Do not insert the &amp;Table of Contents as a page at the start of the document</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>