opened excel process

Pavel Olifer <[email protected]>
Newsgroups gmane.comp.python.windows
Message-ID <CALOnRhcuK2yX4FFr0+LXE37vWqkmrXwOYM72rq+F5L3mNY=qDA@mail.gmail.com>
good morning!

In my project I use:
python == 3.8
openpyxl == 3.0.5
pywin32 == 228
sometimes after printing excel, the window of the excel process is still
open.
may be somebody faced with the same problem.
I include 2 samples of code which are used in the project.


[image: image.png]

from openpyxl import load_workbookimport osimport app_config as
configimport printers.printers as pfrom datetime import datetimeimport
shutilimport time

class EditExcelTemplate:
    def __init__(self, template_name):
        now = datetime.now()
        report_name = "_{}{}{}_{}{}{}_{}".format(now.year, now.month, now.day,
                                              now.hour, now.minute, now.second,
                                              now.microsecond)
        self.report_path = config.EXCEL_REPORT_PATH.format(template_name +
                                                           report_name)

        shutil.copy(src=config.EXCEL_TEMPLATE_PATH.format(template_name),
                    dst=self.report_path)

        # self.wb =
load_workbook(filename=config.EXCEL_TEMPLATE_PATH.format(template_name))
        start_load = time.time()
        self.wb = load_workbook(filename=self.report_path,
                                keep_links=False,
                                keep_vba=False,
                                data_only=True)
        end_load = time.time()
        print('LOAD WORKBOOK|{}'.format(str(end_load - start_load)))
        self.ws = self.wb.active
        self.answer = {'file_name': template_name.upper()}

    def write_workbook(self, row_dest, column_dest, value):
        c = self.ws.cell(row=row_dest, column=column_dest)
        c.value = value


    def save_excel(self):
        self.wb.save(self.report_path)

    def print_excel(self, printer_no):
        p.print_excel(printer_no=printer_no, path_to_file=self.report_path)

    def print_excel_file(self, printer_name):
        p.print_excel_file(printer_name=printer_name,
path_to_file=self.report_path)


import win32apiimport app_configimport os, time

def print_excel(printer_no, path_to_file):
    printer_name = app_config.PRINTER_NAMES[printer_no]

    win32api.ShellExecute(
        1,
        'printto',
        path_to_file,
        '{}'.format(printer_name),
        '.',
        0
    )

def delete_file(path_to_file, try_count=1):
    if os.path.exists(path=path_to_file):
        file_name = path_to_file.split('\\')[-1]
        while try_count < 60:
            try:
                os.remove(path_to_file)
                print('File {} deleted!'.format(file_name))
                break
            except PermissionError:
                print('Can not delete file {}. Hold {}
sec.'.format(file_name, try_count))
                time.sleep(1.0)
                try_count += 1


-- 
-- po
+7 (977) 004 67 53

_______________________________________________
python-win32 mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-win32
image.png (image/png, 209.3 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.