#-*-coding:utf-8-*-
import shlex
import os
import threading
import multiprocessing
lock1=threading.Lock()
lock2=threading.Lock()
lockm1=multiprocessing.Lock()
lockm2=multiprocessing.Lock()
metaflyweight = lambda name, parents, attrs: type(
name,
parents,
dict(attrs.items() + [
('__instances', dict()),
('__new__', classmethod(
lambda cls, *args, **kargs: cls.__instances.setdefault(
tuple(args),
super(type(cls), cls).__new__(*args, **kargs))
)
)
])
)
class Wpx2Pdf:
def __init__(self):
pass
# 此方法适用于将所有格式的office文件转化为pdf
def all2pdf(infile, outdir):
try:
lockm1.acquire()
lock1.acquire()
command_line ='/usr/bin/libreoffice --headless --invisible --convert-to pdf %s --outdir %s'%(infile, outdir)
callback=os.system(command_line)
if callback:
# 运行成功
print(callback)
else:
msg="转化失败"
if not os.path.exists(infile):
msg+=",%s文件不存在"%str(infile)
lock1.release()
lockm1.release()
except Exception as e:
print(e)
lock1.release()
lockm1.release()
return (404,e)
def converpic(input_file,output_file):
try:
lockm2.acquire()
lock2.acquire()
command_line="convert -density 50 %s[0] %s"%(input_file,output_file)
callback = os.system(command_line)
if callback:
# 运行成功
print(callback)
lock2.release()
lockm2.release()
return output_file
except Exception as e:
print(e)
lock2.release()
lockm2.release()
return (404,e)
def tif2pdf(infile,outfile):
try:
lockm1.acquire()
lock1.acquire()
command_line ='convert %s +compress %s'%(infile,outfile)
callback=os.system(command_line)
if callback:
# 运行成功
print(callback)
else:
msg="转化失败"
if not os.path.exists(infile):
msg+=",%s文件不存在"%str(infile)
lock1.release()
lockm1.release()
except Exception as e:
print(e)
lock1.release()
lockm1.release()
return (404,e)
if __name__=="__main__":
p2=Wpx2Pdf()
print(p2.all2pdf('/root/Downloads/wode1.xlsx','/root/Downloads/'))
python将所有格式的office文件转化为pdf
最新推荐文章于 2024-01-16 16:43:11 发布