V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Herwifte
V2EX  ›  Python

使用 flask 控制 libreoffice 同时转换多个 docx 文件出错

  •  
  •   Herwifte · 2019-08-11 22:25:44 +08:00 · 1280 次点击
    这是一个创建于 1691 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在在开发一个网站,获取客户表单上传的 docx 文件,然后通过 libreoffice 转换成 pdf。 现在测试发现如果多个用户同时上传文件,libreoffice 可以正常转换(在后台已经看到转换后的 pdf 文件),但是总有一个或几个用户返回 FileNotFoundError: [Errno 2] No such file or directory 错误,使用 redis 做消息队列仍然不能解决,求教原因?

    相关代码如下:

    upload_path = os.path.join(parentdir, 'static/Upload_Files/BeforeSwitchFile/', new_filename)
    print_file.save(upload_path)
               
    # 将数据添加到列表中
    switch = q.enqueue_call(switch_topdf(upload_path))
    if switch.func_name == 'success':
    	i = new_filename.rindex('.')
        new_filename = new_filename[:i] + '.pdf'
        switched_dir = os.path.join(parentdir, 'static/Upload_Files',
             secure_filename(new_filename))  # 转换 pdf 后的文件路径
        # 读取文件页数
        pageCount = read_pdf_pages(switched_dir)
        cost = pageCount * print_cost +0.2
    

    switch_topdf 函数代码

    def switch_topdf(filename):
    
        cmd = "soffice --headless --convert-to pdf:writer_pdf_Export {} --outdir {}".format(filename, FileSaveDir)  # libreoffice 转换命令
        print(cmd)
        try:
            returnCode = subprocess.call(cmd, shell=True)
            # returnCode = os.system(cmd)
            if returnCode != 0:
                raise IOError("{} failed to switch".format(filename))
        except Exception:
            return 'error'
        else:
            return 'success'
    
    bakabie
        1
    bakabie  
       2019-08-12 04:39:17 +08:00 via Android
    会不会是因为 secure_filename 处理后的文件路径找不到的原因?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   988 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:12 · PVG 04:12 · LAX 13:12 · JFK 16:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.