-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (100 loc) · 3.62 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import sys
from pathlib import Path
# import openpyxl
from subprocess import Popen
from PyQt5 import QtWidgets
from PySide2.QtWidgets import QApplication, QWidget
# from checkword import checkdir
from termcolor import colored
from Scripts.ui_uploadfiles import Ui_Upload
# from tkinter.messagebox import showinfo
class Uploadfiles(QWidget, Ui_Upload):
def __init__(self) -> None:
super(Uploadfiles, self).__init__()
self.setupUi(self)
self.setWindowTitle("Convert To PDF")
self.BuscarPlanilha.clicked.connect(lambda: self.openfile())
self.listfiles = []
self.IniciarUpload.clicked.connect(
lambda: self.initbot(input_filename=self.listfiles)
)
def openfile(self):
input_filename = QtWidgets.QFileDialog.getOpenFileNames(
filter="Arquivo DOCX (*.docx)"
)[0]
for filename in input_filename:
self.listfiles.append(filename)
input_filenames_str = "\n".join(input_filename)
self.lineEndr.setText(input_filenames_str)
def initbot(self, input_filename):
for filename in input_filename:
self.p = filename
docx_name = filename
self.pdfinsoffice(namefile=docx_name)
def pdfinsoffice(self, namefile):
path = "c:\\Program Files\\"
name = "soffice.exe"
print(colored('Convertendo arquivo "{a}"...'.format(a=namefile), "yellow"))
if sys.platform == "linux":
currentdir = Path(self.p).parent.resolve()
arquivo_de_entrada = os.path.join(
currentdir, "{infile}".format(infile=namefile)
)
outfile = os.path.join(currentdir)
print(colored('Convertendo arquivo "{a}"...'.format(a=namefile), "blue"))
try:
p = Popen(
[
"soffice",
"--headless",
"--convert-to",
"pdf",
"--outdir",
outfile,
arquivo_de_entrada,
]
)
p.communicate()
print(colored("Documento Convertido com Sucesso!", "green"))
except Exception:
print(
colored(
'Não foi possível converter o arquivo "{b}"'.formar(b=namefile),
"red",
)
)
else:
LIBRE_OFFICE = ""
for root, dirs, files in os.walk(path):
if name in files:
LIBRE_OFFICE = os.path.join(root, name)
currentdir = Path(self.p).parent.resolve()
arquivo_de_entrada = namefile
outfile = os.path.join(currentdir)
try:
p = Popen(
[
LIBRE_OFFICE,
"--headless",
"--convert-to",
"pdf",
"--outdir",
outfile,
arquivo_de_entrada,
]
)
p.communicate()
print(colored("Documento Convertido com Sucesso!", "green"))
except Exception:
print(
colored(
'Não foi possível converter o arquivo "{b}"'.formar(b=namefile),
"red",
)
)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Uploadfiles()
window.show()
app.exec_()