2
\$\begingroup\$

I'm looking for feedback on functionality, PEP8, advice, etc as a learning tool. I'm not sure how to tell if this program should be broken into separate functions or combined as one.

While hand editing files for an organization I work for, I made this code having seen an opportunity to simplify "How to Integrate Excel Data with Word".

Here are 4 templates simultaneously populated for output documents:

All files are on Google including output files, if that's easier. Git

import csv

def docgen(filename_csv='docgen'):
    open_csv = open(str(filename_csv) + '.csv')
    read_csv = csv.reader(open_csv)
    templates = []
    labels = []
    for row in read_csv:
        if 'docs out' in row[1].lower():
            labels = row[2:]
        elif 'templates in' in row[0].lower():
            templates = row[1:]
            templates = [x for x in templates if x]
        else:
            all_templates = templates[:]
            data_row = row[2:]
            if row[0] != '':
                all_templates.extend([row[0]])
            for template in all_templates:
                if template != '':
                    open_template = open(template + '.txt')
                    doctemp(open_template, template, labels, 
                                 data_row, row)
                    open_template.close()

def doctemp(open_template, template, labels, data_row, row):
    doc_filename = str(row[1]) + ' ' + str(template) + '.txt'
    open_doc = open(doc_filename, 'w')
    for template_line in open_template.readlines():
        doc_line = str(template_line)
        for i, variable in enumerate(labels):
            data = data_row[i]
            data = str(data).strip()
            variable = str(variable).strip()
            if data == '':
                variable_after = variable + ' '
                variable_before = ' ' + variable
                variable_around = ' ' + variable + ' '
                if variable_after in doc_line:
                    doc_line = doc_line.replace(variable_after, '')
                if variable_before in doc_line:
                    doc_line = doc_line.replace(variable_before, '')
                if variable_around in doc_line:
                    doc_line = doc_line.replace(variable_around, '')
                else:
                    doc_line = doc_line.replace(variable, '')
                continue
            if 'units' in variable:
                units = variable.replace(' units', '')
                if data == '1':
                    if units[-1] == 's':
                        units = units[:-1]
                doc_line = doc_line.replace(variable, data + ' ' + units)
            else:
                doc_line = doc_line.replace(variable, data)
        open_doc.write(doc_line)
    open_doc.close()

docgen()

<table>
    <tr>
        <td>Templates in</td>
        <td>template all</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td>Docs out</td>
        <td>title</td>
        <td>first name</td>
        <td>last name</td>
        <td>address 1</td>
        <td>address 2</td>
        <td>dogs units</td>
        <td>donations units</td>
        <td>taxable $</td>
        <td>Organization</td>
        <td>Phone</td>
    </tr>
    <tr>
        <td></td>
        <td>LiamRodriguez</td>
        <td>Mr.</td>
        <td>Liam</td>
        <td>Rodriguez</td>
        <td>100 Main St.</td>
        <td>"Sao Paulo</td>
        <td> Brazil 01000"</td>
        <td></td>
        <td>1</td>
        <td>$100 </td>
        <td>Vets Abroad</td>
        <td>406-243-0211</td>
    </tr>
    <tr>
        <td>special template 1</td>
        <td>LucasPerez PhD.</td>
        <td>Dr.</td>
        <td>Lucas</td>
        <td>Perez PhD.</td>
        <td>200 Main St.</td>
        <td>"Santa Catarina</td>
        <td> Brazil 88000"</td>
        <td>4</td>
        <td>1</td>
        <td>$500 </td>
        <td>Vets Abroad</td>
        <td>406-243-0211</td>
    </tr>
    <tr>
        <td></td>
        <td>NoahGonzalez</td>
        <td>Mr.</td>
        <td>Noah</td>
        <td>Gonzalez</td>
        <td>300 Main St.</td>
        <td>"Rio de Janeiro</td>
        <td> Brazil 20000"</td>
        <td>1</td>
        <td></td>
        <td>$100 </td>
        <td>Vets Abroad</td>
        <td>406-243-0211</td>
    </tr>
    <tr>
        <td>special template 2</td>
        <td>Hernandez</td>
        <td>Dr. & Sir. Ethan</td>
        <td></td>
        <td>Hernandez</td>
        <td>400 Main St. </td>
        <td>"Paraiba</td>
        <td> Brazil 58000"</td>
        <td>5</td>
        <td>2</td>
        <td>$700 </td>
        <td>Vets Abroad</td>
        <td>406-243-0211</td>
    </tr>
    <tr>
        <td></td>
    </tr>
</table>

\$\endgroup\$
1
\$\begingroup\$

Looping

The block of code:

if data == '':
    variable_after = variable + ' '
    variable_before = ' ' + variable
    variable_around = ' ' + variable + ' '
    if variable_after in doc_line:
        doc_line = doc_line.replace(variable_after, '')
    if variable_before in doc_line:
        doc_line = doc_line.replace(variable_before, '')
    if variable_around in doc_line:
        doc_line = doc_line.replace(variable_around, '')
    else:
        doc_line = doc_line.replace(variable, '')
    continue

can be simplified and shortened with a loop:

if not data:  # '' is False so use `not` to check for empty string
    for spaced_var in [variable + ' ',
                       ' ' + variable,
                       ' ' + variable + ' ']:
        if spaced_var in doc_line:
            doc_line = doc_line.replace(spaced_var, '')
    else:
        doc_line = doc_line.replace(variable, '')
    continue
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.