python-3.x “PageObject”对象没有属性“drawText

kmbjn2e3  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(297)

我试图制作一个PDF翻译器,但在我的代码过程中,出现了这个错误:
追溯(最近一次调用):文件“c:\Users\Geovane\Desktop\TranslatePDF\translate_pdf.py”,line 71,in translate_pdf translated_page.drawText(10,10,translated_page_text)AttributeError:“PageObject”对象没有属性“drawText”
我不知道还能做什么
下面是我的代码:

import sys
import PyPDF2
from time import sleep
from PySide6 import QtWidgets
from googletrans import Translator

class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PDF Translator")
        self.resize(400, 200)

        self.select_pdf_button = QtWidgets.QPushButton("Selecionar PDF")
        self.select_pdf_button.clicked.connect(self.select_pdf)

        self.languages_combo = QtWidgets.QComboBox()
        self.languages_combo.addItem("Inglês", "en")
        self.languages_combo.addItem("Português", "pt")
        self.languages_combo.addItem("Espanhol", "es")

        self.translate_button = QtWidgets.QPushButton("Traduzir")
        self.translate_button.clicked.connect(self.translate_pdf)
        self.translate_button.setEnabled(False)

        self.progress_bar = QtWidgets.QProgressBar()
        self.progress_bar.setRange(0, 100)
        self.progress_bar.hide()

        # Criar o layout
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.select_pdf_button)
        layout.addWidget(self.languages_combo)
        layout.addWidget(self.translate_button)
        layout.addWidget(self.progress_bar)

        self.pdf_path = ""

    def select_pdf(self):
        file_dialog = QtWidgets.QFileDialog()
        file_dialog.setNameFilter("PDF (*.pdf)")
        if file_dialog.exec_():
            self.pdf_path = file_dialog.selectedFiles()[0]
            self.translate_button.setEnabled(True)

    def translate_pdf(self):
        dest_language, ok_pressed = QtWidgets.QInputDialog.getItem(
            window,
            "Selecionar idioma",
            "Idioma destino:",
            ["pt", "en", "es"],
            0,
            False
        )

        if ok_pressed:
            translator = Translator()

            with open(self.pdf_path, 'rb') as pdf_file:
                pdf_reader = PyPDF2.PdfReader(pdf_file)
                pdf_writer = PyPDF2.PdfWriter()

                for page_num in range(len(pdf_reader.pages)):
                    page = pdf_reader.pages[page_num]
                    page_text = page.extract_text()

                    translated_page_text = translator.translate(page_text, dest=dest_language).text

                    translated_page = PyPDF2.PageObject.create_blank_page(pdf_writer, width=page.mediabox.width, height=page.mediabox.height)

                    translated_page.merge_page(page)
                    translated_page.drawText(10, 10, translated_page_text)

                    pdf_writer.addPage(translated_page)

                new_file_path, _ = QtWidgets.QFileDialog.getSaveFileName(window, "Salvar PDF", "", "PDF (*.pdf)")

                if new_file_path:
                    with open(new_file_path, 'wb') as new_file:
                        pdf_writer.write(new_file)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

我输入这个只是为了填充这个字段

cx6n0qe3

cx6n0qe31#

据我所知,PageObject没有这个属性或任何用于添加文本的属性。但是,这里是一个代码,用于将文本添加到PDF文件,它是基于您的示例代码:

translated_page_text ='Test Text'
                packet = io.BytesIO()
                can = canvas.Canvas(packet, pagesize=letter)
                can.drawString(290, 720, str(translated_page_text))
                can.save()
                
                packet.seek(0)
                new_pdf = PyPDF2.PdfReader(packet)
                # read your existing PDF
                existing_pdf = PyPDF2.PdfReader(pdf_file)
                output = PyPDF2.PdfWriter()

                page.merge_page(new_pdf.pages[0])
                output.add_page(page)

                with open("new_file_path.pdf", 'wb') as new_file:
                    output .write(new_file)

另外,你可以在Stackoverflow上查看this question

相关问题