Python tạo báo cáo PDF

Nếu công việc của bạn liên quan đến việc tạo báo cáo PDF, hóa đơn, v.v. bạn có thể đã nghĩ về việc tự động hóa điều đó với Python. Python có một số thư viện tuyệt vời để làm việc với tệp PDF, cho phép bạn đọc và viết tệp PDF từ tập lệnh. Nhưng bạn cũng có thể sử dụng các thư viện này làm công cụ GUI đơn giản cơ bản, giúp bạn dễ dàng tự động điền hoặc chỉnh sửa báo cáo PDF trên màn hình

Trong hướng dẫn này, chúng ta sẽ sử dụng hai thư viện để tạo bộ điền báo cáo PDF tùy chỉnh. Dữ liệu sẽ được thu thập bằng biểu mẫu Qt. chỉ cần chỉnh sửa các trường, nhấn "Tạo" để nhận biểu mẫu đã điền trong thư mục. Hai thư viện chúng ta sẽ sử dụng ở đây là --

  • reportlab cho phép bạn tạo các tệp PDF bằng cách sử dụng văn bản và bản vẽ nguyên thủy
  • pdfrw một thư viện để đọc và trích xuất các trang từ các tệp PDF hiện có

Mặc dù chúng tôi có thể sử dụng reportlab để vẽ toàn bộ tệp PDF, nhưng việc thiết kế mẫu bằng các công cụ bên ngoài sẽ dễ dàng hơn và sau đó chỉ cần phủ nội dung động lên mẫu này. Chúng tôi có thể sử dụng

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
6 để đọc tệp PDF mẫu của mình và sau đó trích xuất một trang, sau đó chúng tôi có thể vẽ lên đó bằng cách sử dụng
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
7. Điều đó cho phép chúng tôi phủ thông tin tùy chỉnh [từ ứng dụng của chúng tôi] trực tiếp lên mẫu PDF hiện có mà chúng tôi lưu dưới tên mới

Trong ví dụ này, chúng tôi đang nhập các trường theo cách thủ công nhưng bạn có thể sửa đổi ứng dụng để đọc dữ liệu cho tệp PDF từ tệp CSV bên ngoài và tạo nhiều tệp PDF từ tệp đó

Mẫu PDF

Để thử nghiệm, tôi đã tạo mẫu báo cáo TPS tùy chỉnh bằng Google Tài liệu và tải trang xuống dưới dạng PDF. Trang chứa một số trường sẽ được điền. Trong hướng dẫn này, chúng tôi sẽ viết một biểu mẫu PyQt mà người dùng có thể điền vào và sau đó viết dữ liệu đó ra PDF ở đúng vị trí

Mẫu ở định dạng A4. Lưu nó vào cùng thư mục với tập lệnh của bạn

Nếu bạn có một mẫu khác mà bạn muốn sử dụng, vui lòng sử dụng mẫu đó. Chỉ cần nhớ rằng bạn sẽ cần điều chỉnh vị trí của các trường biểu mẫu khi viết nó

Bố trí dạng xem Biểu mẫu

Qt bao gồm bố cục

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
8 giúp đơn giản hóa quy trình tạo bố cục biểu mẫu đơn giản. Nó hoạt động tương tự như một lưới, nhưng bạn có thể thêm các hàng phần tử lại với nhau và các chuỗi được chuyển đổi tự động thành các đối tượng
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
9. Ứng dụng khung của chúng tôi, bao gồm bố cục đầy đủ phù hợp với biểu mẫu mẫu [ít nhiều] được hiển thị bên dưới

  • PyQt5
  • PySide2
  • PyQt6
  • PySide6

trăn

from PyQt5.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox

class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]

trăn

from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]

Khi viết các công cụ để thay thế/tự động hóa các biểu mẫu giấy, bạn nên thử bắt chước bố cục của biểu mẫu giấy sao cho quen thuộc.

Ở trên sẽ cho chúng ta bố cục như sau trong một cửa sổ khi chạy. Bạn đã có thể nhập nội dung vào các trường, nhưng việc nhấn nút sẽ không làm được gì cả -- chúng tôi chưa viết mã để tạo tệp PDF hoặc nối nó với nút

Tạo PDF

Để tạo PDF bằng mẫu cơ sở, chúng tôi sẽ kết hợp

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
7 và
from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
1. Quá trình này như sau --

  1. Đọc trong tệp
    from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec_[]
    
    2 bằng cách sử dụng
    from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec_[]
    
    1 và chỉ trích xuất trang đầu tiên
  2. Tạo một đối tượng reportlab
    from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec_[]
    
    4
  3. Sử dụng
    from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec_[]
    
    5 để tạo đối tượng canvas, sau đó thêm đối tượng đó vào Canvas bằng
    from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec_[]
    
    6
  4. Vẽ ra các bit tùy chỉnh trên Canvas
  5. Lưu tệp PDF vào tệp

Mã được hiển thị bên dưới, mã này không yêu cầu Qt, bạn có thể lưu vào một tệp và chạy nguyên trạng. Khi chạy tệp PDF kết quả sẽ được lưu dưới dạng

from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
7 trong cùng một thư mục

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
6

Vì quá trình tạo PDF đang thực hiện IO nên có thể mất một chút thời gian [e. g. nếu chúng tôi tải tệp từ ổ đĩa mạng]. Do đó, tốt hơn là xử lý vấn đề này trong một chủ đề riêng biệt. Tiếp theo, chúng ta sẽ xác định trình chạy chuỗi tùy chỉnh này

Chạy thế hệ trong một chủ đề riêng biệt

Vì mỗi thế hệ là một công việc riêng biệt, nên sử dụng khung công tác

from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
8 của Qt để xử lý quy trình -- điều này cũng giúp đơn giản hóa việc thêm các mẫu có thể tùy chỉnh cho mỗi công việc sau này. Chúng tôi đang sử dụng cùng một cách tiếp cận đã thấy trong hướng dẫn Đa luồng nơi chúng tôi sử dụng một lớp con của
from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
8 để giữ mã chạy tùy chỉnh của chúng tôi và triển khai các tín hiệu dành riêng cho người chạy trên một lớp con riêng biệt của
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
60

  • PyQt5
  • PySide2
  • PyQt6
  • PySide6

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
0

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
1

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
2

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
3

Chúng tôi đã xác định hai tín hiệu ở đây

  • from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    61 phát ra tên tệp của tệp PDF đã lưu [khi thành công]
  • from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    62 phát ra lỗi dưới dạng chuỗi để gỡ lỗi

Chúng tôi cần một

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
63 để thêm trình chạy tùy chỉnh của chúng tôi vào. Chúng tôi có thể thêm phần này vào khối
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
64 của chúng tôi trong khối
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
65

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
9

Bây giờ chúng ta đã xác định trình tạo

from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
8, chúng ta chỉ cần triển khai phương thức
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
67 để tạo trình chạy, truyền dữ liệu từ các trường biểu mẫu của chúng ta và bắt đầu chạy trình tạo

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
0

Trong mã này, trước tiên chúng tôi vô hiệu hóa

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
68 để người dùng không thể nhấn nút nhiều lần trong khi quá trình tạo đang diễn ra. Sau đó, chúng tôi xây dựng một từ điển dữ liệu từ các vật dụng của mình, sử dụng phương pháp
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
69 để lấy văn bản từ các vật dụng của
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
00,
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
01 để lấy giá trị từ
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
02 và
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
03 để lấy biểu diễn văn bản thuần túy của
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
04. Chúng tôi chuyển đổi giá trị số thành một chuỗi, vì chúng tôi đang đặt văn bản

Để thực sự tạo tệp PDF, chúng tôi tạo một phiên bản của trình chạy

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
05 mà chúng tôi vừa xác định, chuyển vào từ điển dữ liệu. Chúng tôi kết nối tín hiệu
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
61 với phương thức
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
07 của chúng tôi [được xác định ở dưới cùng, nhưng chưa làm gì cả] và tín hiệu
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
62 với hàm
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
09 tiêu chuẩn của Python. điều này sẽ tự động in bất kỳ lỗi nào ra bàn điều khiển

Cuối cùng, chúng tôi lấy phiên bản

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
05 của mình và chuyển nó sang phương thức
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
11 của nhóm luồng của chúng tôi để xếp hàng cho nó chạy [nó sẽ bắt đầu ngay lập tức]. Sau đó, chúng tôi có thể kết nối phương thức này với nút của chúng tôi trong
from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
65 của cửa sổ chính của chúng tôi. g

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
1

Nếu bạn chạy ứng dụng ngay bây giờ, việc nhấn nút sẽ kích hoạt quá trình tạo tệp PDF và kết quả sẽ được lưu dưới dạng

from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
7 trong cùng thư mục khi bạn khởi động ứng dụng. Cho đến nay, chúng tôi chỉ đặt một khối văn bản duy nhất trên trang, vì vậy hãy hoàn thành trình tạo để viết tất cả các trường của chúng tôi vào đúng vị trí

hoàn thiện máy phát điện

Tiếp theo, chúng ta cần hoàn thành việc đặt văn bản trên mẫu. Mẹo ở đây là tìm ra khoảng cách trên mỗi dòng cho mẫu của bạn [tùy thuộc vào kích thước phông chữ, v.v. ] và sau đó tính toán các vị trí tương ứng với dòng đầu tiên. Các tọa độ y tăng lên trên trang [vì vậy 0,0 là dưới cùng bên trái] vì vậy trong mã của chúng tôi trước đó, chúng tôi xác định

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
14 cho dòng trên cùng và sau đó trừ 28 cho mỗi dòng

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
2

gói

Đối với hầu hết các trường biểu mẫu của chúng tôi, chúng tôi chỉ có thể xuất văn bản nguyên trạng vì không có ngắt dòng. Nếu văn bản đã nhập quá dài thì nó sẽ tràn -- nhưng nếu muốn, chúng tôi có thể giới hạn điều này trên chính các trường bằng cách đặt độ dài tối đa bằng ký tự, e. g

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
3

Đối với trường nhận xét, mọi thứ phức tạp hơn một chút. Trường có thể dài hơn nhiều và các dòng cần được bao bọc trên nhiều dòng trong mẫu. Trường này cũng chấp nhận ngắt dòng [bằng cách nhấn Enter] gây ra sự cố khi ghi ra PDF

Như bạn có thể thấy trong ảnh chụp màn hình ở trên, các ngắt dòng xuất hiện dưới dạng các ô vuông màu đen trong văn bản. Tin tốt là chỉ cần loại bỏ các ngắt dòng sẽ giúp bạn dễ dàng ngắt dòng hơn. chúng ta chỉ có thể bọc từng dòng thành một số ký tự được chỉ định

Vì các ký tự có chiều rộng thay đổi nên điều này không hoàn hảo, nhưng nó không thành vấn đề. Nếu chúng ta ngắt dòng cho một dòng có đầy đủ các ký tự rộng nhất [W] thì bất kỳ dòng thực nào cũng phù hợp

Python đi kèm với thư viện

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
15 được tích hợp sẵn, mà chúng ta có thể sử dụng để ngắt dòng văn bản của mình sau khi chúng ta đã loại bỏ các dòng mới

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
4

Nhưng chúng ta cần tính đến việc dòng đầu tiên ngắn hơn, điều mà chúng ta có thể thực hiện bằng cách gói đầu tiên đến độ dài ngắn hơn, nối lại phần còn lại và gói lại, e. g

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
5

Các dấu nhận xét trên các dòng bao quanh [45 & 55] hiển thị chiều dài bao cần thiết để vừa một dòng chữ W vào khoảng trống. Đây là dòng ngắn nhất có thể, nhưng không thực tế. Các giá trị được sử dụng sẽ hoạt động với hầu hết văn bản bình thường

Để làm điều này đúng cách, chúng ta nên tính toán kích thước thực tế của từng độ dài văn bản trong phông chữ tài liệu và sử dụng kích thước đó để thông báo cho trình bao bọc

Khi chúng tôi đã chuẩn bị xong các dòng, chúng tôi có thể in chúng lên PDF bằng cách lặp qua danh sách và giảm vị trí y cho mỗi lần. Khoảng cách giữa các dòng trong tài liệu mẫu của chúng tôi là 28

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
6

Điều này cho kết quả sau với một số văn bản lorem ipsum mẫu

Hơn 10.000 nhà phát triển đã mua Tạo ứng dụng GUI bằng Python & Qt

Thông tin thêm Nhận sách

Để hỗ trợ các nhà phát triển ở [[ countryRegion ]], tôi giảm giá [[ localizedDiscount[couponCode] ]]% với mã [[ couponCode ]] — Tận hưởng

Đối với [[ activeDiscount. mô tả ]] Tôi đang đưa ra [[ activeDiscount. giảm giá ]]% giảm giá với mã [[ couponCode ]] — Tận hưởng

Cũng có sẵn qua GumroadLeanpub

Tự động hiển thị kết quả

Khi tệp được tạo, trình chạy của chúng tôi sẽ trả về tên tệp của tệp đã tạo trong một tín hiệu [hiện tại nó luôn giống nhau]. Sẽ rất tuyệt nếu trình bày bản PDF kết quả cho người dùng một cách tự động để họ có thể kiểm tra xem mọi thứ có ổn không. Trên Windows, chúng tôi có thể sử dụng

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
16 để mở tệp bằng trình khởi chạy mặc định cho loại đó -- trong trường hợp này là mở tệp PDF bằng trình xem PDF mặc định

Vì tính năng này không khả dụng trên các nền tảng khác nên chúng tôi sẽ phát hiện lỗi và thay vào đó hiển thị

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
17

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
7

Hoàn thành mã

Mã hoàn chỉnh cho PyQt5, PySide2, PyQt6 hoặc PySide6 được hiển thị bên dưới

  • PyQt5
  • PySide2
  • PyQt6
  • PySide6

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
8

trăn

from PySide2.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
9

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
0

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
1

Tạo từ tệp CSV

Trong ví dụ trên bạn cần gõ dữ liệu để điền thủ công. Điều này tốt nếu bạn không có nhiều tệp PDF để tạo, nhưng sẽ không thú vị lắm nếu bạn có toàn bộ tệp CSV có giá trị dữ liệu để tạo báo cáo cho. Trong ví dụ bên dưới, thay vì trình bày danh sách các trường biểu mẫu cho người dùng, chúng tôi chỉ yêu cầu tệp CSV nguồn từ đó có thể tạo tệp PDF -- mỗi hàng trong tệp tạo một tệp PDF riêng bằng cách sử dụng dữ liệu trong tệp

  • PyQt5
  • PySide2
  • PyQt6
  • PySide6

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
2

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
3

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
4

trăn

from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec[]
5

Bạn có thể chạy ứng dụng này bằng cách sử dụng

from PySide6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox


class Window[QWidget]:

    def __init__[self]:
        super[].__init__[]

        self.name = QLineEdit[]
        self.program_type = QLineEdit[]
        self.product_code = QLineEdit[]
        self.customer = QLineEdit[]
        self.vendor = QLineEdit[]
        self.n_errors = QSpinBox[]
        self.n_errors.setRange[0, 1000]
        self.comments = QTextEdit[]

        self.generate_btn = QPushButton["Generate PDF"]

        layout = QFormLayout[]
        layout.addRow["Name", self.name]
        layout.addRow["Program Type", self.program_type]
        layout.addRow["Product Code", self.product_code]
        layout.addRow["Customer", self.customer]
        layout.addRow["Vendor", self.vendor]
        layout.addRow["No. of Errors", self.n_errors]

        layout.addRow["Comments", self.comments]
        layout.addRow[self.generate_btn]

        self.setLayout[layout]


app = QApplication[[]]
w = Window[]
w.show[]
app.exec_[]
2 và tệp CSV ví dụ này để tạo một vài báo cáo TPS

Những điều cần chú ý --

  • Bây giờ chúng tôi tạo nhiều tệp, vì vậy sẽ không có ý nghĩa gì khi mở chúng khi chúng kết thúc. Thay vào đó, chúng tôi luôn hiển thị thông báo "hoàn thành" và chỉ một lần. Tín hiệu
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    61 đã được đổi tên thành
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    20 và chúng tôi đã xóa tên tệp
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    21 vì nó không còn được sử dụng nữa
  • from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    00 để lấy tên tệp bị tắt nên không thể chỉnh sửa trực tiếp. cách duy nhất để đặt tệp CSV nguồn là chọn tệp trực tiếp, đảm bảo tệp ở đó
  • Chúng tôi tự động tạo tên tệp đầu ra, dựa trên tên tệp nhập và số hàng hiện tại.
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    23 được lấy từ CSV đầu vào. với tệp CSV có tên là
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    24 sẽ có tên là
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    25,
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    26, v.v. Các tệp được ghi ra thư mục chứa CSV nguồn
  • Vì một số hàng/tệp có thể thiếu các trường bắt buộc, chúng tôi sử dụng
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    27 trên từ điển hàng với một chuỗi trống mặc định

cải tiến có thể

Nếu bạn muốn cải thiện mã này, có một vài điều bạn có thể thử

  • Tạo mẫu và vị trí tệp đầu ra có thể định cấu hình - sử dụng hộp thoại tệp Qt
  • Tải các vị trí trường từ một tệp cùng với mẫu [JSON] để bạn có thể sử dụng cùng một biểu mẫu với nhiều mẫu
  • Làm cho các trường có thể định cấu hình được -- điều này khá phức tạp, nhưng bạn cần nhập các loại cụ thể [
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    21,
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    29,
    from PyQt6.QtWidgets import QPushButton, QLineEdit, QApplication, QFormLayout, QWidget, QTextEdit, QSpinBox
    
    
    class Window[QWidget]:
    
        def __init__[self]:
            super[].__init__[]
    
            self.name = QLineEdit[]
            self.program_type = QLineEdit[]
            self.product_code = QLineEdit[]
            self.customer = QLineEdit[]
            self.vendor = QLineEdit[]
            self.n_errors = QSpinBox[]
            self.n_errors.setRange[0, 1000]
            self.comments = QTextEdit[]
    
            self.generate_btn = QPushButton["Generate PDF"]
    
            layout = QFormLayout[]
            layout.addRow["Name", self.name]
            layout.addRow["Program Type", self.program_type]
            layout.addRow["Product Code", self.product_code]
            layout.addRow["Customer", self.customer]
            layout.addRow["Vendor", self.vendor]
            layout.addRow["No. of Errors", self.n_errors]
    
            layout.addRow["Comments", self.comments]
            layout.addRow[self.generate_btn]
    
            self.setLayout[layout]
    
    
    app = QApplication[[]]
    w = Window[]
    w.show[]
    app.exec[]
    
    30, v.v. ] có thể có các vật dụng cụ thể được gán cho chúng

Rất tốt, bạn đã hoàn thành hướng dẫn này. Đánh dấu là hoàn thành

[[ người dùng. hoàn thành. độ dài ]] đã hoàn thành [[ người dùng. vệt+1 ]] vệt ngày

Để có hướng dẫn đầy đủ về xây dựng ứng dụng GUI bằng Python, hãy xem hướng dẫn PyQt6 của chúng tôi. Sử dụng thư viện khác?

Python có thể tạo báo cáo không?

Tổng quan về báo cáo Python . Tin vui là Python có thể tạo báo cáo ở tất cả các định dạng này . Vì vậy, bạn có thể chọn bất kỳ định dạng nào trong số này, tùy thuộc vào nhu cầu của người dùng báo cáo. Dưới đây là tóm tắt những gì chúng tôi sẽ trình bày trong hướng dẫn này.

Tôi có thể cạo PDF Python không?

Với sự trợ giúp của thư viện python, chúng tôi có thể tiết kiệm thời gian và tiền bạc bằng cách tự động hóa quy trình trích xuất dữ liệu từ tệp PDF và chuyển đổi dữ liệu phi cấu trúc thành dữ liệu bảng .

Chủ Đề