Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destination Output and Unit Testing #18

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions csv2docx/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click
import csv2docx.csv2docx as c2d
import click



@click.command()
Expand All @@ -14,6 +15,10 @@
@click.option(
'--delimiter', '-d',
default=";",
help='delimiter used in your csv. Default is \';\'')
def main(data, template, delimiter):
c2d.convert(data, template, delimiter)
help='Delimiter used in your csv. Default is \';\'')
@click.option(
'--destination', '-dest',
default="output",
help='The destination to store the files.')
def main(data, template, delimiter, destination):
c2d.convert(data, template, delimiter, destination)
34 changes: 27 additions & 7 deletions csv2docx/csv2docx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import csv
from mailmerge import MailMerge
def convert(data, template, delimiter=";"):
print ("Getting .docx template and .csv data files ...")
import csv
import pathlib


def absolute_path(path: str) -> pathlib.Path:
"""Creates a path to store output data if it does not exists.

This method will create the directory if non-existent.

Args:
path: the path from user in any format (relative, absolute, etc.)
Returns:
A path to store output data.
"""
path = pathlib.Path(path)
if not path.exists():
path.mkdir(parents=True, exist_ok=True)
return path


def convert(data, template, delimiter=";", destination="output"):
print("Getting .docx template and .csv data files ...")

with open(data, 'rt') as csvfile:
csvdict = csv.DictReader(csvfile, delimiter=delimiter)
Expand All @@ -16,15 +35,16 @@ def convert(data, template, delimiter=";"):
# see if all fields are accounted for in the .csv header
column_in_data = set(docx_mergefields) - set(csv_headers)
if len(column_in_data) > 0:
print (f"{column_in_data} is in the word document, but not csv.")
print(f"{column_in_data} is in the word document, but not csv.")
return

print("All fields are present in your csv. Generating Word docs ...")

path = absolute_path(destination)

for counter, row in enumerate(csvdict):
# Must create a new MailMerge for each file
docx = MailMerge(template)
single_document = {key : row[key] for key in docx_mergefields}
single_document = {key: row[key] for key in docx_mergefields}
docx.merge_templates([single_document], separator='page_break')
# TODO: write to user-defined subfolder
docx.write(f"{counter}.docx")
docx.write(f"{path}/{counter}.docx")