Filter chapters flag: "--chapters".
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Dan Helfman 2022-07-28 09:36:00 -07:00
parent 949e316fe7
commit e66348bb82
2 changed files with 31 additions and 0 deletions

View File

@ -21,6 +21,20 @@ following formatting, modifying the file in-place:
* enable page numbering starting on the second page (upper right)
### Chapter excerpting
You can optionally filter the output to only include certain chapters by
number. For instance:
```bash
$ format-novel manuscript.docx --chapters 5 6 7
```
This will only include chapters 5, 6, and 7 in the output file, excluding all
other chapters. For this to work, the input document must indicate chapter
titles with the "Heading 2" style.
## Planned features
* Support for specifying author and project title to show up in the header

View File

@ -6,6 +6,21 @@ import sys
import docx
def filter_chapters(document, chapter_numbers):
if not chapter_numbers:
return
chapter_number = 0
for paragraph in document.paragraphs:
if paragraph.style.name == 'Heading 2':
chapter_number += 1
if chapter_number not in chapter_numbers:
element = paragraph._element
element.getparent().remove(element)
def set_default_font(document):
document.styles['Normal'].font.name = 'Times New Roman'
@ -86,6 +101,7 @@ def parse_arguments(unparsed_arguments):
'--chapters',
metavar='CHAPTER',
nargs='+',
type=int,
help='Numbers of chapters to include in output'
)
@ -96,6 +112,7 @@ def main():
arguments = parse_arguments(sys.argv[1:])
document = docx.Document(arguments.document_filename)
filter_chapters(document, arguments.chapters)
set_default_font(document)
# add_header_text(document, 'Author / Project Title / ')
add_header_page_number(document)