Initial import.
This commit is contained in:
commit
9be8f179f0
83
format-novel
Executable file
83
format-novel
Executable file
@ -0,0 +1,83 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
|
||||
import docx
|
||||
|
||||
|
||||
def set_default_font(document):
|
||||
document.styles['Normal'].font.name = 'Times New Roman'
|
||||
|
||||
|
||||
def add_header_text(document, text):
|
||||
document.sections[0].header.paragraphs[0].text = text
|
||||
|
||||
|
||||
def create_element(name):
|
||||
return docx.oxml.OxmlElement(name)
|
||||
|
||||
|
||||
def create_attribute(element, name, value):
|
||||
element.set(docx.oxml.ns.qn(name), value)
|
||||
|
||||
|
||||
def add_header_page_number(document):
|
||||
run = document.sections[0].header.paragraphs[0].add_run()
|
||||
|
||||
fldChar1 = create_element('w:fldChar')
|
||||
create_attribute(fldChar1, 'w:fldCharType', 'begin')
|
||||
|
||||
instrText = create_element('w:instrText')
|
||||
create_attribute(instrText, 'xml:space', 'preserve')
|
||||
instrText.text = "PAGE"
|
||||
|
||||
fldChar2 = create_element('w:fldChar')
|
||||
create_attribute(fldChar2, 'w:fldCharType', 'end')
|
||||
|
||||
run._r.append(fldChar1)
|
||||
run._r.append(instrText)
|
||||
run._r.append(fldChar2)
|
||||
|
||||
|
||||
def skip_page_number_on_first_page(document):
|
||||
document.sections[0].different_first_page_header_footer = True
|
||||
page_number_type = docx.oxml.OxmlElement('w:pgNumType')
|
||||
page_number_type.set(docx.oxml.ns.qn('w:start'), "0")
|
||||
document.sections[0]._sectPr.append(page_number_type)
|
||||
|
||||
|
||||
def right_align_header(document):
|
||||
header_paragraph = document.sections[0].header.paragraphs[0]
|
||||
header_paragraph.paragraph_format.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.RIGHT
|
||||
|
||||
def double_space_and_indent(paragraphs):
|
||||
this_paragraph_plaintext = False
|
||||
previous_paragraph_plaintext = False
|
||||
|
||||
for paragraph in paragraphs:
|
||||
paragraph.paragraph_format.line_spacing = 2
|
||||
this_paragraph_plaintext = bool(paragraph.style.name == 'Normal')
|
||||
|
||||
# Indent first lines of paragraphs except for styled ones (headings, etc.) and initial
|
||||
# paragraphs.
|
||||
if this_paragraph_plaintext and previous_paragraph_plaintext:
|
||||
paragraph.paragraph_format.first_line_indent = docx.shared.Inches(0.5)
|
||||
|
||||
previous_paragraph_plaintext = this_paragraph_plaintext
|
||||
|
||||
|
||||
def main(document_filename):
|
||||
document = docx.Document(document_filename)
|
||||
|
||||
set_default_font(document)
|
||||
#add_header_text(document, 'Author / Project Title / ')
|
||||
add_header_page_number(document)
|
||||
skip_page_number_on_first_page(document)
|
||||
right_align_header(document)
|
||||
double_space_and_indent(document.paragraphs)
|
||||
|
||||
document.save(document_filename)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv[1])
|
Loading…
x
Reference in New Issue
Block a user