12038陌 发表于 7 天前

发票批量打印

发票批量打印工具
2月12日更新:

更新内容:
1、纸张大小新增A5四版和A5两版(A5进纸方向为竖向)
2、增加打印预览功能(有个小bug,部分自定义纸张预览图与实际打印效果会不一致)
3、可选OFD格式打印(下载Spire.Pdf.dll,不下载则pdf和图片格式依旧可以正常打印)(单个OFD文件页面不超过3页)。
4、增加多版打印图片之间的间隙。
5、评论区反馈部分pdf打印机出现卡死,经测试是因为这些打印机调用了windows文件对话框保存pdf文件,由于软件使用了多线程,在打印子线程中调用模态对话框就会出现线程冲突,现修复Adobe PDF和Microsoft Print to PDF打印机出现的冲突;
其他打印机解决方式1:点击界面最下方:使用系统打印对话框进行打印 。
方式2:找到该打印机设置界面-打印机首选项-设置指定保存位置不弹出对话框(如果有)。
方式3:从打印预览界面进行打印。
方式4:更换虚拟打印机(论坛搜索的几款都还不错)。
6、应评论区需求增加同一发票打印两版在一张A4纸上的功能,使用方法:勾选‘复制页面*2’,设置纸张大小‘A4两版’。
7、文件列表中可按Delete键删减选中。
重复一遍:如果使用虚拟打印机推荐勾选合并页面(生成一个文件)。






1月16日更新:
可选A4三版 和自定义纸张大小打印
自定义纸张大小最多保留3个,超过3个最后添加的会覆盖最先添加的





1月12日更新:
可选A4 四版和A4 六版 打印
可选添加裁剪线







PDF/图片打印
文件路径传入方式:拖入文件或者文件夹,双击列表输入路径等。
虚拟打印机推荐合并页面(生成一个文件),否则不合并页面(打印机响应速度会快那么一点)
A5进纸方向为竖向:148mmX210mm(宽X高)
请先少量测试页面方向是否正确,设置是否生效,再批量打印。
64位,不支持win7
蓝奏:https://wwvv.lanzout.com/b00l1hq06d 密码:52pj

风之影赫 发表于 7 天前


import sys
import os
import time
import fitz
from print import Ui_PrintForm
from itertools import count
from pathlib import Path
import configparser
from PIL.ImageQt import ImageQt
from PIL import Image
from PySide6.QtGui import QPainter, QPageSize, QPageLayout, QColor
from PySide6.QtPrintSupport import QPrinter, QPrintDialog, QPrinterInfo
from PySide6.QtCore import QRect, QObject, QThread, Signal, QEvent, Qt
from PySide6.QtWidgets import QApplication, QWidget, QListWidgetItem, QFileDialog, QStyleFactory

class printPdfWorker(QObject):
    finished = Signal()
    progress = Signal(str, str)

    def __init__(self, pdf=None, parent=None):
      super().__init__()
      self.win = parent
      self.paths = pdf
      self.cup = self.win.cup
      self.paper = self.win.paper
      self.printname = self.win.printName
      self.dpi = int(str(self.win.dpi)[:3])
      self.orientation = self.win.dirtion()
      ali = self.win.ali
      if '水平居中' in ali:
            self.inter = 2
      else:# inserted
            if '靠右居中' in ali:
                self.inter = 1
      self.double = self.win.double
      self._printer = QPrinter(QPrinter.PrinterMode.HighResolution)
      self._printer.setPrinterName(self.printname)
      if 'A4' in self.paper:
            self._printer.setPageSize(QPageSize(QPageSize.A4))
            self.height_dpx, self.width_dpx = self.a4_size(self.dpi, 210, 297)
      else:# inserted
            if 'A5' in self.paper:
                self._printer.setPageSize(QPageSize(QPageSize.A5))
                self.height_dpx, self.width_dpx = self.a4_size(self.dpi, 148, 210)
      self._printer.setPrintRange(QPrinter.PrintRange.AllPages)
      if self.win.checkbox.isChecked():
            self._printer.setColorMode(QPrinter.ColorMode.GrayScale)
      else:# inserted
            self._printer.setColorMode(QPrinter.ColorMode.Color)
      try:
            self._printer.setDuplex(self.double)
      except Exception as e:
            pass# postinserted
      else:# inserted
            if self.orientation:
                self._printer.setPageOrientation(self.orientation)
      self._printer.setCopyCount(self.cup)
            print(e)

    def setprinton(self):
      if 'A4' in self.paper:
            self._printer.setPageSize(QPageSize(QPageSize.A4))
            self.height_dpx, self.width_dpx = self.a4_size(self.dpi, 210, 297)
      else:# inserted
            if 'A5' in self.paper:
                self._printer.setPageSize(QPageSize(QPageSize.A5))
                self.height_dpx, self.width_dpx = self.a4_size(self.dpi, 148, 210)
      self._printer.setPrintRange(QPrinter.PrintRange.AllPages)
      if self.win.checkbox.isChecked():
            self._printer.setColorMode(QPrinter.ColorMode.GrayScale)
      else:# inserted
            self._printer.setColorMode(QPrinter.ColorMode.Color)
      try:
            self._printer.setDuplex(self.double)
      except Exception as e:
            pass# postinserted
      else:# inserted
            if self.orientation:
                self._printer.setPageOrientation(self.orientation)
      self._printer.setCopyCount(self.cup)
            print(e)
      else:# inserted
            pass

    def rundialog(self):
      self.dialog = QPrintDialog(self._printer)
      self.dialog.setOptions(QPrintDialog.PrintToFile | QPrintDialog.PrintSelection)
      if self.dialog.exec():
            self.runprint()
      else:# inserted
            self.finished.emit()

    def runprint(self):
      """长时间运行的打印任务。"""# inserted
      try:
            self.setprinton()
            if self.paper!= 'A4两版':
                if self.win.mergebox.isChecked():
                  painter = QPainter(self._printer)
                  rect = painter.viewport()
                  images = self.add_image(self.paths)
                  for pil_image, pageNumber in zip(images, count(1)):
                        if pageNumber > 1:
                            self._printer.newPage()
                        self.print_image(pil_image, rect, painter)
                  painter.end()
                else:# inserted
                  for index, path in enumerate(self.paths):
                        painter = QPainter(self._printer)
                        rect = painter.viewport()
                        images = []
                        path = Path(path)
                        suffix = path.suffix.lower()
                        if suffix == '.pdf':
                            images = self.open_pdf(path, images)
                            for pil_image, pageNumber in zip(images, count(1)):
                              if pageNumber > 1:
                                    self._printer.newPage()
                              self.print_image(pil_image, rect, painter)
                        else:# inserted
                            with Image.open(path) as image:
                              pass# postinserted
      except Exception as e:
                              pil_image = image.copy()
                              self.print_image(pil_image, rect, painter)
                        painter.end()
            else:# inserted
                if self.win.mergebox.isChecked():
                  images = self.add_image(self.paths)
                  self.A4_sep(images)
                else:# inserted
                  batch_size = 10
                  for i in range(0, len(self.paths), batch_size):
                        batch_paths = self.paths
                        images = self.add_image(batch_paths)
                        self.A4_sep(images)
            self.progress.emit('文件已发送至打印机', 'green')
      else:# inserted
            self.finished.emit()
                print(f'打印出错{e}0')

    def open_pdf(self, path, images):
      with fitz.open(path) as pdf:
            num_pages = len(pdf)
            printRange = range(num_pages)
            page_indices =
            for index in page_indices:
                pixmap = pdf.get_pixmap(dpi=self.dpi)
                pil_image = Image.frombytes('RGB', , pixmap.samples)
                images.append(pil_image)
            return images

    def add_image(self, paths=None):
      images = []
      file_paths = paths
      file_paths = +
      for index, path in enumerate(file_paths):
            path = Path(path)
            suffix = path.suffix.lower()
            if suffix == '.pdf':
                images = self.open_pdf(path, images)
            else:# inserted
                with Image.open(path) as image:
                  images.append(image.copy())
      return images

    def A4_sep(self, images):
      if len(images) % 2!= 0:
            images.append(None)
      painter = QPainter(self._printer)
      for index, pageNumber in zip(range(0, len(images), 2), count(1)):
            image1 = images
            image2 = images
            if pageNumber > 1:
                self._printer.newPage()
            self.join_pic(image1, image2, painter)
      painter.end()

    def print_image(self, pil_image, rect, painter):
      pilWidth, pilHeight = pil_image.size
      imageRatio = pilHeight / pilWidth
      viewportRatio = rect.height() / rect.width()
      A4Ratio = self.height_dpx / self.width_dpx
      if self.win.up == '自动旋转':
            if viewportRatio < 1 and imageRatio > 1 or (viewportRatio > 1 and imageRatio < 1):
                pil_image = pil_image.transpose(Image.ROTATE_90)
                pilWidth, pilHeight = pil_image.size
                imageRatio = pilHeight / pilWidth
            if A4Ratio < imageRatio:
                x = int(pilHeight / viewportRatio - pilWidth)
                xOffset = int(x / 2)
                yOffset = 0
            else:# inserted
                xOffset = 0
                y = int(rect.height() - rect.width() / pilWidth * pilHeight)
                yOffset = int(y / self.inter)
      else:# inserted
            xOffset, yOffset, x, y = (0, 0, 0, 0)
      if viewportRatio > imageRatio:
            y = int(rect.width() / (pilWidth / pilHeight))
            printArea = QRect(xOffset, yOffset, rect.width(), y)
      else:# inserted
            x = int(pilWidth / pilHeight * rect.height())
            printArea = QRect(xOffset, yOffset, x, rect.height())
      image = ImageQt(pil_image)
      painter.drawImage(printArea, image)
      return painter

    def join_pic(self, image1, image2, painter):
      if image2 == None:
            image2 = Image.new('RGB', image1.size, 'white')
      for image in (image1, image2):
            pilWidth, pilHeight = image.size
            imageRatio = pilHeight / pilWidth
            if imageRatio > 1:
                if image == image1:
                  image1 = image.transpose(Image.ROTATE_90)
                if image == image2:
                  image2 = image.transpose(Image.ROTATE_90)

      def resize_image(image):
            height = int(self.height_dpx / 2)
            ratio = height / image.size
            max_width = int(image.size * ratio)
            max_height = int(height)
            if max_width > self.width_dpx:
                max_width = self.width_dpx
                ratio = self.width_dpx / image.size
                max_height = int(image.size * ratio)
            new_width = max_width
            new_height = max_height
            resized_image = image.resize((new_width, new_height))
            return resized_image
      image1 = resize_image(image1)
      image2 = resize_image(image2)
      half_hight = int(self.height_dpx / 2)
      merged_image = Image.new('RGB', (self.width_dpx, self.height_dpx), 'white')
      if image1.size < self.width_dpx:
            x1 = int((self.width_dpx - image1.size) / self.inter)
      else:# inserted
            x1 = 0
      if image1.size < half_hight:
            y1 = int((half_hight - image1.size) / 2)
      else:# inserted
            y1 = 0
      if image2.size < self.width_dpx:
            x2 = int((self.width_dpx - image2.size) / self.inter)
      else:# inserted
            x2 = 0
      if image2.size < half_hight:
            y2 = int((half_hight - image2.size) / 2)
      else:# inserted
            y2 = 0
      merged_image.paste(image1, (x1, y1))
      merged_image.paste(image2, (x2, half_hight + y2))
      rect = painter.viewport()
      self.print_image(merged_image, rect, painter)

    def a4_size(self, dpi, width, height):
      a4_width = width / 25.4
      a4_height = height / 25.4
      height_dpx = int(a4_height * dpi)
      width_dpx = int(a4_width * dpi)
      return (height_dpx, width_dpx)

class Window(QWidget):
    def __init__(self):
      super(Window, self).__init__()
      self.ui = Ui_PrintForm()
      self.ui_win = self.windowFlags()
      self.ui.setupUi(self)
      self.longRunningBtn = self.ui.pushButton
      self.longRunningBtn.clicked.connect(self.runPrintTask)
      self.addfile = self.ui.pushButton_2
      self.addfile.clicked.connect(self.getFile)
      self.clearfile = self.ui.pushButton_3
      self.clearfile.clicked.connect(self.clearFile)
      self.sysPrint = self.ui.toolButton
      self.sysPrint.clicked.connect(self.rundio)
      self.spinbox = self.ui.doubleSpinBox
      self.paper_box = self.ui.comboBox
      self.dpi_box = self.ui.comboBox_2
      self.double_box = self.ui.comboBox_3
      self.alignment = self.ui.comboBox_4
      self.direction = self.ui.comboBox_6
      self.paper_box.currentIndexChanged.connect(self.setdirection)
      self.bar = self.ui.label_8
      self.listwidget = self.ui.listWidget
      self.checkbox = self.ui.checkBox
      self.mergebox = self.ui.checkBox_2
      self.printbox = self.ui.comboBox_5
      self.load_printers()
      self.small_win = self.ui.dockWidget
      self.small_win.hide()
      self.textbox = self.ui.textEdit
      self.textbox.textChanged.connect(self.changedText)
      self.load_config()
      self.setdirection()
      self.file_path = []
      self.listwidget.viewport().installEventFilter(self)

    def eventFilter(self, source, event):
      if event.type() == QEvent.MouseButtonDblClick and source is self.listwidget.viewport():
            self.small_win.show()
            return True
      return super().eventFilter(source, event)

    def setdirection(self):
      if self.paper_box.currentText()!= 'A4':
            self.direction.setCurrentIndex(0)
            self.direction.setEnabled(False)
      else:# inserted
            self.direction.setEnabled(True)

    def changedText(self):
      self.clearFile()
      text = self.textbox.toPlainText()
      lines = text.splitlines()
      for line in lines:
            if line.strip():
                self.showListwidget(line)

    def load_config(self):
      config = configparser.ConfigParser()
      config.read('printConfig.ini')
      self.spinbox.setValue(int(config.get('Print', 'Series', fallback=1)))
      self.paper_box.setCurrentIndex(int(config.get('Print', 'Paper', fallback=0)))
      self.dpi_box.setCurrentIndex(int(config.get('Print', 'Dpi', fallback=1)))
      self.double_box.setCurrentIndex(int(config.get('Print', 'Double', fallback=0)))
      self.double_box.setCurrentIndex(int(config.get('Print', 'Center', fallback=0)))
      self.printbox.setCurrentText(config.get('Print', 'PrintName', fallback=''))
      self.direction.setCurrentIndex(int(config.get('Print', 'PageDirection', fallback=0)))
      self.checkbox.setCheckState(Qt.CheckState.Checked if config.getboolean('Print', 'Color', fallback=False) else Qt.CheckState.Unchecked)
      self.mergebox.setCheckState(Qt.CheckState.Checked if config.getboolean('Print', 'Mergebox', fallback=False) else Qt.CheckState.Unchecked)

    def doublePrint(self):
      double = self.double_box.currentIndex()
      if double == 0:
            return QPrinter.DuplexMode.DuplexNone
      if double == 1:
            return QPrinter.DuplexLongSide
      if double == 2:
            return QPrinter.DuplexShortSide
      if double == 3:
            return QPrinter.DuplexAuto

    def dirtion(self):
      self.up = self.direction.currentText()
      if self.up == '纵向':
            return QPageLayout.Portrait
      if self.up == '横向':
            return QPageLayout.Landscape

    def clearFile(self):
      self.file_path = []
      self.listwidget.clear()
      self.runBar('准备就绪......', 'black')

    def getFile(self):
      response = QFileDialog.getOpenFileNames(parent=self, caption='选择文件', filter='文件类型 (*.pdf *.jpg *.png *.jpeg *.bmp);;Images (*.png *.jpg *.jpeg *.bmp);;PDF Files (*.pdf)')
      if response:
            file_paths = response
            for path in file_paths:
                self.showListwidget(path)

    def showListwidget(self, path):
      self.file_path.append(path)
      item_widget = QListWidgetItem(path)
      self.listwidget.addItem(item_widget)
      self.bar.setText(f'已添加文件:{len(self.file_path)}个')
      return self.file_path

    def lianjie(self, paths):
      self.clearFile()
      valid_extensions = {'.jpg', '.png', '.jpeg', 'bmp', '.pdf'}
      for path in paths:
            _, extensions = os.path.splitext(path)
            if extensions.lower() in valid_extensions:
                self.showListwidget(path)

    def load_printers(self):
      printers = QPrinterInfo.availablePrinters()
      printer_names =
      self.printbox.addItems(printer_names)

    def printdata(self):
      self.cup = self.spinbox.value()
      self.paper = self.paper_box.currentText()
      self.dpi = self.dpi_box.currentText()
      self.ali = self.alignment.currentText()
      self.double = self.doublePrint()
      self.printName = self.printbox.currentText()
      self.runBar('正在发送页面到打印机\n请勿关闭程序...', 'red')

    def rundio(self):
      pdf_file = self.file_path
      if not pdf_file:
            self.runBar('没有待打印的文件', 'blue')
            return
      self.printdata()
      self.thread = QThread()
      self.worker = printPdfWorker(pdf_file, self)
      self.worker.moveToThread(self.thread)
      self.thread.started.connect(self.worker.rundialog)
      self.worker.finished.connect(self.thread.quit)
      self.worker.finished.connect(self.worker.deleteLater)
      self.worker.progress.connect(self.runBar)
      self.thread.start()

    def runPrintTask(self):
      pdf_file = self.file_path
      if not pdf_file:
            self.runBar('没有待打印的文件', 'blue')
            return
      self.printdata()
      self.thread = QThread()
      self.worker = printPdfWorker(pdf_file, self)
      self.worker.moveToThread(self.thread)
      self.thread.started.connect(self.worker.runprint)
      self.worker.finished.connect(self.thread.quit)
      self.worker.finished.connect(self.worker.deleteLater)
      self.worker.progress.connect(self.runBar)
      self.thread.start()

    def runBar(self, text, color='black'):
      palette = self.bar.palette()
      palette.setColor(self.bar.foregroundRole(), QColor(color))
      self.bar.setPalette(palette)
      self.bar.setText(text)

    def dragEnterEvent(self, event):
      if event.mimeData().hasUrls():
            event.accept()
      else:# inserted
            event.ignore()

    def dropEvent(self, event):
      valid_extensions = {'.jpg', '.png', '.jpeg', 'bmp', '.pdf'}
      dropped_files = []
      for url in event.mimeData().urls():
            file_path = url.toLocalFile()
            if os.path.isdir(file_path):
                for root, dirs, files in os.walk(file_path):
                  for file in files:
                        full_file_path = os.path.join(root, file)
                        _, extension = os.path.splitext(full_file_path)
                        if extension.lower() in valid_extensions:
                            dropped_files.append(full_file_path)
            else:# inserted
                _, extension = os.path.splitext(file_path)
                if extension.lower() in valid_extensions:
                  dropped_files.append(file_path)
      for file_path in dropped_files:
            self.showListwidget(file_path)

    def closeEvent(self, event):
      self.save_combobox()
      event.accept()

    def save_combobox(self):
      config = configparser.ConfigParser()
      config.read('printConfig.ini')
      if 'Print' not in config:
            config.add_section('Print')
      cup = self.spinbox.value()
      paper = self.paper_box.currentIndex()
      dpi = self.dpi_box.currentIndex()
      double = self.double_box.currentIndex()
      center = self.alignment.currentIndex()
      printName = self.printbox.currentText()
      direction = self.direction.currentIndex()
      mergebox = int(self.mergebox.checkState() == Qt.CheckState.Checked)
      config['Print'] = {'Series': int(cup), 'Paper': str(paper), 'Dpi': str(dpi), 'Double': str(double), 'Center': str(center), 'Color': str(int(self.checkbox.checkState() == Qt.CheckState.Checked)), 'PrintName': str(printName), 'PageDirection': str(direction), 'Mergebox': str(mergebox)}
      with open('printConfig.ini', 'w') as configfile:
            config.write(configfile)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle(QStyleFactory.create('Fusion'))
    win = Window()
    win.show()
    sys.exit(app.exec())

寒哥Gh61ac8 发表于 7 天前

你的需求版本已经上传了网盘了,打印居中方式选择‘’垂直两端‘’

风之影赫 发表于 7 天前

实测 A4两版 从中间裁剪后 上下俩部分 过大
提个意见
A4两版 时候,上下部分分别 向上和向下靠齐 也就是两端缩进些
中间区域留白增加
会更好

huoxianghui913 发表于 7 天前

支持楼主的分享精神,

huoxianghui913 发表于 7 天前

2张发票合并页面打印,并不是选择合并页面,而是纸张大小选A4两版。还有2个问题,能不能自由删除添加进去序列的文件,和输出PDF版自己保存。

huoxianghui913 发表于 7 天前

感谢,这个是神级需求啊,谢谢

风之影赫 发表于 7 天前

有空用一下谢谢分享

寒哥Gh61ac8 发表于 7 天前

省时省力,感谢

风之影赫 发表于 7 天前

非常好,谢谢,收藏了
页: [1] 2
查看完整版本: 发票批量打印