python将所有格式的office文件转化为pdf


#-*-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/'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值