<pre><code>import sys
import os
import comtypes.client
from pdf2docx import Converter
from docx2pdf import convert
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QToolTip, QFileDialog, QLineEdit, QLabel
from PyQt6.QtGui import QIcon, QFont
from pathlib import Path
home_directory = str(Path.home()).replace("/", "\\")
def name(file):
path = file
i = 1
while os.path.exists(path):
path = file
path += " " + str(i)
i += 1
return path
class MainGui(QWidget):
def __init__(self):
super().__init__()
self.row1 = 50
self.row2 = 100
self.row3 = 175
self.row4 = 260
self.button_font = QFont('Verdana', 10)
self.line = QLineEdit(self)
self.browse = QPushButton("Durchsuchen", self)
self.selected_files_count = QLabel("0 Elemente ausgewählt", self)
self.docx_files = []
self.pdf_files = []
self.init_me()
def init_me(self):
font = QFont('Verdana', 8)
font.setBold(True)
QToolTip.setFont(font)
pdf_to_docx_button = QPushButton('PDF Dateien auswählen', self)
pdf_to_docx_button.setFont(self.button_font)
pdf_to_docx_button.setToolTip('öffen Sie einen Datei-Dialog, um Dateien auszuwählen')
pdf_to_docx_button.move(50, self.row1)
pdf_to_docx_button.setFixedWidth(200)
pdf_to_docx_button.setFixedHeight(35)
pdf_to_docx_button.clicked.connect(self.pdf_to_docx_button_pressed)
docx_to_pdf_button = QPushButton('DOCX Dateien auswählen', self)
docx_to_pdf_button.setFont(self.button_font)
docx_to_pdf_button.setToolTip('öffen Sie einen Datei-Dialog, um Dateien auszuwählen')
docx_to_pdf_button.move(300, self.row1)
docx_to_pdf_button.setFixedWidth(200)
docx_to_pdf_button.setFixedHeight(35)
docx_to_pdf_button.clicked.connect(self.docx_to_pdf_button_pressed)
self.line.move(50, self.row3)
self.line.setFixedWidth(320)
self.line.setFixedHeight(30)
self.line.setFont(self.button_font)
self.line.setText(home_directory)
self.browse.move(390, self.row3)
self.browse.setFont(self.button_font)
self.browse.setFixedWidth(110)
self.browse.setFixedHeight(30)
self.browse.setToolTip('öffen Sie einen Datei-Dialog, um den Ausgabe-Ordner auszuwählen')
self.browse.clicked.connect(self.browse_button_pressed)
convert_button = QPushButton('Konvertieren', self)
convert_button.move(190, self.row4)
convert_button.setFixedWidth(170)
convert_button.setFixedHeight(50)
convert_button.setFont(self.button_font)
convert_button.clicked.connect(self.convert)
self.selected_files_count.move(200, self.row2)
self.selected_files_count.setFont(self.button_font)
self.selected_files_count.setFixedSize(200, 50)
self.move(50, 50)
self.setFixedSize(550, 400)
self.setWindowTitle("Konverter")
self.setWindowIcon(QIcon(r"icon.png"))
self.show()
def pdf_to_docx_button_pressed(self):
fd = QFileDialog()
fd.setAcceptMode(QFileDialog.AcceptMode.AcceptOpen)
path = fd.getOpenFileNames(self, "PDF Dateien öffnen", home_directory, "PDF (*.pdf)")
fd.close()
print(len(path[0]))
if len(path[0]) > 0:
self.pdf_files.extend(path[0])
print(True)
self.update_label()
def docx_to_pdf_button_pressed(self):
fd = QFileDialog()
fd.setAcceptMode(QFileDialog.AcceptMode.AcceptOpen)
path = fd.getOpenFileNames(self, "Docx Dateien öffnen", home_directory, "Word-Dokument ("
"*.docx)")
fd.close()
if len(path[0]) > 0:
self.docx_files.extend(path[0])
self.update_label()
def convert(self):
directory = self.line.text()
if not os.path.exists(directory):
return
if not os.path.isdir(directory):
return
for file in self.pdf_files:
docx_file = name(directory + "/" + os.path.basename(file).replace(".pdf", ".docx"))
convert_pdf_to_docx(file, docx_file)
update_docx(docx_file)
for file in self.docx_files:
convert_docx_to_pdf(file, name(directory + "/" + os.path.basename(file).replace(".docx", ".pdf")))
self.docx_files = []
self.pdf_files = []
def browse_button_pressed(self):
fd = QFileDialog()
fd.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
path = fd.getExistingDirectory(self, "Ausgabeordner auswählen", home_directory)
if path is not None and not len(path) <= 0:
self.line.setText(path)
def update_label(self):
count = len(self.pdf_files) + len(self.docx_files)
if count != 1:
self.selected_files_count.setText(str(count) + " Elemente ausgewählt")
else:
self.selected_files_count.setText("ein Element ausgewählt")
def convert_pdf_to_docx(pdf_file, docx_file):
cv = Converter(pdf_file)
cv.convert(docx_file, start=0, end=None)
cv.close()
def convert_docx_to_pdf(docx_file, pdf_file):
convert(docx_file, pdf_file)
def update_docx(file):
docx_format_name = 16
file_in = os.path.abspath(file)
file_out = file_in.replace(".docx", "2.docx")
word_application = comtypes.client.CreateObject('word.Application')
if word_application is not None:
doc = word_application.Documents.Open(file_in)
doc.SaveAs(file_out, FileFormat=docx_format_name)
doc.Close()
word_application.Quit()
os.remove(file_in)
os.rename(file_out, file_in)
app = QApplication(sys.argv)
w = MainGui()
sys.exit(app.exec())
</code></pre>
<p>这是我的密码</p>
<p>命令是:</p>
<pre><code>pyinstaller --noconfirm --onefile --windowed "C:/Users/cpuhv/Documents/Projekte/Python/Python Test/PDFConverter.py"
</code></pre>