-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertdocxtopdf.py
151 lines (123 loc) · 4.72 KB
/
convertdocxtopdf.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import sys
from pathlib import Path
from subprocess import Popen
import comtypes.client
import openpyxl
from termcolor import colored
from checkword import checkdir
nomexls = input(colored("Informe a Planilha: ", "yellow"))
class ConvertWordPDF:
def initbot(self, inputfile):
self.p = inputfile
wrkbk_input = openpyxl.load_workbook(filename=self.p)
sheet_input = wrkbk_input.active
for i in range(1, sheet_input.max_row + 1):
cell_obj = sheet_input.cell(row=i, column=1)
if cell_obj.value is not None and cell_obj.value != "":
docx_name = sheet_input.cell(row=i, column=1).value
wordir = checkdir().find_all(
exefile="winword.exe", pathfile=r"c:/Program Files"
)
if wordir is True:
try:
self.convertpdf(namefile=docx_name)
except Exception as e:
print(e)
else:
self.pdfinsoffice(namefile=docx_name)
if i == sheet_input.max_row:
print("fim do processo")
os.system("PAUSE")
def convertpdf(self, namefile):
wdFormatPDF = 17
currentdir = Path(self.p).parent.resolve()
inputfile = os.path.join(currentdir, "{infile}".format(infile=namefile))
print(inputfile)
removedocx = inputfile.replace(".docx", ".pdf")
outpatch = removedocx
# Cria instancia de um objeto COM para manipular Documentos Word
word = comtypes.client.CreateObject("Word.Application")
# Carrega Arquivo de entrada (.doc)
doc = word.Documents.Open(inputfile)
print(colored('Convertendo arquivo "{a}"...'.format(a=namefile), "yellow"))
try:
# Salva arquivo de saida em formato .pdf
doc.SaveAs(outpatch, FileFormat=wdFormatPDF)
# Fecha arquivo de Entrada
doc.Close()
# Finaliza instancia do Objeto COM criado
word.Quit()
print(colored("Documento Convertido com Sucesso!", "green"))
except Exception:
print(
colored(
'Não foi possível converter o arquivo "{b}"'.formar(b=namefile),
"red",
)
)
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(__file__).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(__file__).parent.resolve()
arquivo_de_entrada = os.path.join(
currentdir, "{infile}".format(infile=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",
)
)
start = ConvertWordPDF()
start.initbot()