Commit d7ca8c98 authored by Anthony Jacob's avatar Anthony Jacob
Browse files

use .env to check config folders and files

parent a1dce9bd
Loading
Loading
Loading
Loading

.env

0 → 100644
+3 −0
Original line number Diff line number Diff line
EXCEL_FILE=C:\Users\antho\dev_workspace\script-invoice\Recettes.xlsx
INVOICE_FOLDER=C:\Users\antho\dev_workspace\script-invoice\FACTURES
TEMPLATE_WORD=C:\Users\antho\dev_workspace\script-invoice\template.docx
+49 −34
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import locale
from docx import Document
from datetime import datetime
from openpyxl import load_workbook
from dotenv import load_dotenv


HEADER_INDEX = {
@@ -25,7 +26,7 @@ def main():
    print("------ génération des factures ------")

    print("------ ouverture du fichier Recettes.xlsx ------")
    excel = load_workbook(filename=os.path.abspath("Recettes.xlsx"))
    excel = load_workbook(filename=os.getenv('EXCEL_FILE'))

    month_choice = ""
    print(excel.sheetnames)
@@ -39,8 +40,11 @@ def main():

    worksheet = excel.worksheets[month_choice_int]

    start_line = line_input("numéro de ligne de debut: ") - 1
    end_line = line_input("numéro de ligne de fin: ", worksheet.max_row)
    start_line = line_input("numéro de ligne de debut (minimum 2) (maximum " + str(worksheet.max_row) + ") : ",
                            min_allowed=2,
                            max_allowed=worksheet.max_row) - 1
    end_line = line_input("numéro de ligne de fin: (maximum " + str(worksheet.max_row) + ") : ",
                          max_allowed=worksheet.max_row)

    word = open_com_word()

@@ -48,29 +52,15 @@ def main():

        invoice_data = process_data(worksheet, row)
        if (invoice_data is not None):

            print("\n")
            check_or_create_folder(invoice_data)

            generate_docx_invoice(invoice_data)
            generate_pdf_invoice(word, invoice_data)

    close_com_word(word)

    # with open('input.csv', newline='') as csvfile:
    #    client_reader = csv.DictReader(csvfile, delimiter=';', quotechar='|')
    #    word = open_com_word()
    #    for row in client_reader:
    #        print("\n\ngénération de la facture pour la ligne")
    #        print(row)
    #        invoice_date = datetime.strptime( row['date'], '%d/%m/%Y')

    #        filename = "Facture NF." + row['num_facture'] + row['prenom'][0] + row['nom']
    #        # filename = invoice_date.strftime('%Y%m%d') + " " + row['nom'] + " " + row['prenom']
    #        # filename = datetime.today().strftime('%Y%m%d') + " " + row['nom'] + " " + 'dest'

    #        generate_docx_invoice(row, filename)

    #        generate_pdf_invoice(word, filename)

    #    close_com_word(word)


def process_data(worksheet, row):
    invoice_data = dict()
@@ -122,15 +112,24 @@ def process_data(worksheet, row):
    #     invoice_data["filename"] = "Facture NF." + str(invoice_data["invoice_number"]) + invoice_data["surname"][0] + invoice_data["name"]
    # else:
    #     invoice_data["filename"] = "Facture NF." + str(invoice_data["invoice_number"]) + invoice_data["surname"] + invoice_data["name"]
    invoice_data["filename"] = "Facture NF." + str(invoice_data["invoice_number"]) + invoice_data["surname"][0] + invoice_data["name"]
    invoice_data["filename"] = "Facture NF." \
        + str(invoice_data["invoice_number"]) \
        + invoice_data["surname"][0] \
        + invoice_data["name"]

    return invoice_data


def generate_docx_invoice(invoice_data):

    invoice_folder = os.getenv("INVOICE_FOLDER")
    folder_name = invoice_data["surname"] + " " + invoice_data["name"]
    full_path = os.path.join(invoice_folder, folder_name)
    full_file_path = os.path.join(full_path, invoice_data["filename"] + ".docx")
    template: Document = Document(os.getenv("TEMPLATE_WORD"))

    print("------ generation du fichier " + invoice_data["filename"] + ".docx ------")
    print(os.path.abspath(invoice_data["filename"] + ".docx"))
    template: Document = Document("template.docx")
    print(full_file_path)

    replace_string_document(template, "XXSURNAMEXX", str(invoice_data["surname"]))
    replace_string_document(template, "XXNAMEXX", str(invoice_data["name"]))
@@ -142,7 +141,7 @@ def generate_docx_invoice(invoice_data):
    replace_string_document(template, "XXDATELONGXX", str(invoice_data["date_long"]))
    replace_string_document(template, "XXINVOICEXX", str(invoice_data["invoice_number"]))

    template.save(invoice_data["filename"] + ".docx")
    template.save(full_file_path)
    del template


@@ -160,16 +159,18 @@ def close_com_word(word):

def generate_pdf_invoice(word, invoice_data):

    print("------ generation du fichier " + invoice_data["filename"] + ".pdf ------")
    invoice_folder = os.getenv("INVOICE_FOLDER")
    folder_name = invoice_data["surname"] + " " + invoice_data["name"]
    full_path = os.path.join(invoice_folder, folder_name)
    full_in_file_path = os.path.join(full_path, invoice_data["filename"] + ".docx")
    full_out_file_path = os.path.join(full_path, invoice_data["filename"] + ".pdf")
    wdFormatPDF = 17

    in_file = os.path.abspath(invoice_data["filename"] + ".docx")
    out_file = os.path.abspath(invoice_data["filename"] + ".pdf")

    print(out_file)
    print("------ generation du fichier " + invoice_data["filename"] + ".pdf ------")
    print(full_out_file_path)

    doc = word.Documents.Open(in_file)
    doc.SaveAs(out_file, FileFormat=wdFormatPDF)
    doc = word.Documents.Open(full_in_file_path)
    doc.SaveAs(full_out_file_path, FileFormat=wdFormatPDF)
    doc.Close()


@@ -194,13 +195,14 @@ def replace_string_paragraph(paragraph, old_text: str, new_text: str):
                inline.text = text


def line_input(message, max_allowed=None):
def line_input(message, max_allowed=None, min_allowed=None):
    correct = False
    while (not correct):
        line = input(message)
        try:
            line = int(line)
            if (max_allowed is not None and line > max_allowed):
            if (max_allowed is not None and line > max_allowed
                    or min_allowed is not None and line < min_allowed):
                raise ValueError
            correct = True
        except ValueError:
@@ -208,5 +210,18 @@ def line_input(message, max_allowed=None):
    return line


def check_or_create_folder(invoice_data):

    invoice_folder = os.getenv("INVOICE_FOLDER")
    folder_name = invoice_data["surname"] + " " + invoice_data["name"]
    full_path = os.path.join(invoice_folder, folder_name)

    if (not os.path.exists(full_path)
            or not os.path.isdir(full_path)):
        os.mkdir(full_path)
        print("le dossier " + full_path + " a été créé")


if __name__ == '__main__':
    load_dotenv()
    main()
+1 −0
Original line number Diff line number Diff line
@@ -5,3 +5,4 @@ lxml==4.9.1
openpyxl==3.0.10
Pillow==9.3.0
python-docx==0.8.11
python-dotenv==0.21.0