[python-win32] opened excel process

Pavel Olifer pavel.olifer at gmail.com
Wed Sep 2 00:26:29 EDT 2020


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20200902/20e7c57c/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 214332 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-win32/attachments/20200902/20e7c57c/attachment-0001.png>


More information about the python-win32 mailing list