在我使用的项目中:
有时用户会打电话给我,告诉他们他们将文档发送到打印机,但是无法打印。 在打印机上出现标签-“忙”。仅在重启打印机后,工作才能进行得更远。 我发布了创建Excel的打印函数和类。
def print_excel_file(printer_name, path_to_file):
win32api.ShellExecute(
0,
'printto',
path_to_file,
'{}'.format(printer_name),
'.',
0
)
from openpyxl import load_workbook
import os
import app_config as config
import printers.printers as p
from datetime import datetime
import shutil
import 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('WORKBOOK|{0}|LOADED|{1}'.format(self.report_path.split('\\')[-1], 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)
self.wb.close()
def print_excel(self, printer_no):
p.print_excel(printer_no=printer_no, path_to_file=self.report_path)