在 Docker 上运行 LibreOffice 转换器

2024-03-16

该问题与使用 LibreOffice 无头转换器自动转换上传的文件有关。出现此错误:

LibreOffice 7 fatal error - Application cannot be started

Ubuntu 版本:21.04

我尝试过的: 从 Azure Blob 存储获取文件, 将其放入 BASE_DIR/Input_file 中, 使用我通过子进程运行的 Linux 命令将其转换为 PDF, 将其放入 BASE_DIR/Output_file 文件夹中。

下面是我的代码:

我正在以这种方式将 LibreOffice 安装到 docker

RUN apt-get update \
&& ACCEPT_EULA=Y apt-get -y install LibreOffice

主要逻辑:

blob_client = container_client.get_blob_client(f"Folder_with_reports/")

with open(os.path.join(BASE_DIR, f"input_files/{filename}"), "wb") as source_file:
    source_file.write(data)

source_file = os.path.join(BASE_DIR, f"input_files/{filename}")  # original docs here
output_folder = os.path.join(BASE_DIR, "output_files")   # pdf files will be here

# assign the command of converting files through LibreOffice
command = rf"lowriter --headless --convert-to pdf {source_file} --outdir {output_folder}"

# running the command
subprocess.run(command, shell=True)

# reading the file and uploading it back to Azure Storage
with open(os.path.join(BASE_DIR, f"output_files/MyFile.pdf"), "rb") as outp_file:
    outp_data = outp_file.read()

blob_name_ = f"test"
container_client.upload_blob(name = blob_name_ ,data = outp_data, blob_type="BlockBlob")

我应该安装吗lowriter代替自由办公室?对于这种操作可以使用 BASE_DIR 吗?我将不胜感激任何建议。


部分解决方案:

在这里,我简化了案例并使用此 Dockerfile 创建了额外的 docker 映像。 我应用了两种方法:unoconv 和直接转换。

Dockerfile:

FROM ubuntu:21.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get -y upgrade && \
    apt-get -y install python3.10 && \
    apt update && apt install python3-pip -y

# Method1 - installing LibreOffice and java
RUN apt-get --no-install-recommends install libreoffice -y
RUN apt-get install -y libreoffice-java-common

# Method2 - additionally installing unoconv
RUN apt-get install unoconv

ARG CACHEBUST=1

ADD BASE.py /code/BASE.py

# copying input doc/docx files to the docker's linux 
COPY /input_files /code/input_files

CMD ["/code/BASE.py"]
ENTRYPOINT ["python3"]

BASE.py

import os
import subprocess

BASE_DIR = "/code"

# subprocess.run("ls code/input_files", shell=True)

for filename in os.listdir('code/input_files'):

    source_file = f"/code/input_files/{filename}"   # original document

    output_filename = os.path.splitext(filename)[0]+".pdf"
    output_file = f"code/output_files/{output_filename}"
    output_folder = "code/output_files"   # pdf files will be here

    # METHOD 1 - LibreOffice straightly
    assign the command of converting files through LibreOffice
    convert_to_pdf = rf"libreoffice --headless --convert-to pdf {source_file} --outdir {output_folder}"
    subprocess.run(r'ls code/output_files/', shell=True)

    ## METHOD 2 - Using unoconv - also working
    # convert_to_pdf = f"unoconv -f pdf {source_file}"
    # subprocess.run(convert_to_pdf, shell=True)
    # print(f'file {filename} converted')

如果构建时文件已经在 Linux 文件系统中,则上述方法可以解决该问题。但在构建docker镜像后仍然没有找到将文件写入系统的方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Docker 上运行 LibreOffice 转换器 的相关文章

  • 使用 OpenCV 和/或 Numpy 对两个图像进行 Alpha 混合 [重复]

    这个问题在这里已经有答案了 我想将一个填充纯色的半透明矩形添加到已加载的半透明 PNG 中 这是我正在使用的输入图像示例 该图像加载了标准cv2 IMREAD UNCHANGED标志 以便完美保留 alpha 通道 该输入图像存储在imag
  • pandas Wide_to_long 后缀参数

    我对在 pandas 中使用 Wide to long 时的参数有疑问 有一个参数叫suffix我不明白 在文档中它说 后缀 str 默认 d 捕获所需后缀的正则表达式 d 捕获数字后缀 没有数字的后缀可以用否定字符类 D 指定 您还可以进
  • 使用应用程序脚本将 MS Word 文件(保存在云端硬盘中)转换为 Google 文档

    我被某些事情困住了 找不到解决办法 有没有办法使用文件 url 或 id 将存储在 Google Drive 中的 MS Word 文件转换为 Google 文档 我目前有一个电子表格 其中包含文件的网址 或者 也可以使用 python 脚
  • ca 证书 Mac OS X

    我需要在emacs 上安装offlineimap 和mu4e 问题是配置 当我运行 Offlineimap 时 我得到 OfflineIMAP 6 5 5 Licensed under the GNU GPL v2 v2 or any la
  • 如何让python优雅地失败?

    我只是想知道如何让 python 在所有可能的错误中以用户定义的方式失败 例如 我正在编写一个处理 大 项目列表的程序 并且某些项目可能不符合我定义的格式 如果 python 检测到错误 它目前只会输出一条丑陋的错误消息并停止整个过程 但是
  • 无法使用 Docker 运行 Strapi

    我正在尝试 Docker 化我的Strapi应用程序 所以首先在项目的根目录中我创建了一个 env文件包含以下内容 HOST 0 0 0 0 PORT 3002 然后 里面backend config server js I have mo
  • Python GTK + webkit - 在 gtk.main() 之后插入 JavaScript

    我在终端中尝试了这个 一切正常 但是如果我在脚本内运行这个 我无法在 gtk main 之后插入 JavaScript import gtk import webkit w gtk Window b webkit WebView w add
  • 组和平均 NumPy 矩阵

    假设我有一个任意的 numpy 矩阵 如下所示 arr 6 0 12 0 1 0 7 0 9 0 1 0 8 0 7 0 1 0 4 0 3 0 2 0 6 0 1 0 2 0 2 0 5 0 2 0 9 0 4 0 3 0 2 0 1 0
  • numpy:大量线段/点的快速规则间隔平均值

    我沿着一维线有许多 约 100 万个 不规则间隔的点 P 这些标记线段 这样 如果点是 0 x a x b x c x d 则线段从 0 gt x a x a gt x b x b gt x c x c gt x d 等 我还有每个段的 y
  • 如何在Python代码中查找列号

    简短问题 当按上述方式调用函数时 我可以找到行号here https stackoverflow com questions 3056048 filename and line number of python script 同样 如何找到
  • 张量流和线程

    下面是来自 Tensorflow 网站的简单 mnist 教程 即单层 softmax 我尝试通过多线程训练步骤对其进行扩展 from tensorflow examples tutorials mnist import input dat
  • 在谷歌云上训练神经网络时出现“无法获取路径的文件系统”错误

    我正在使用 Google Cloud 在云上训练神经网络 如下例所示 https cloud google com blog big data 2016 12 how to classify images with tensorflow u
  • pandas 中连续数据的平行坐标图

    pandas 的 parallel coordinates 函数非常有用 import pandas import matplotlib pyplot as plt from pandas tools plotting import par
  • PyArmor - 打包为一个可执行文件

    当我执行此命令时 您好 使用 PyArmor pyarmor pack main py 它将它打包到一个名为的文件夹中dist里面包含我的 exe 以及许多 Python 扩展文件 据我所知 PyArmor 使用 PyInstaller 来
  • 如何使用 django-pyodbc (ubuntu 16.04) 配置数据库设置 Django-MSSQL?

    我是 Django 新手 目前正在尝试使用另一个数据库来保存我的模型 即MS SQL 我的数据库部署在docker容器中 903876e64b67 microsoft mssql server linux bin sh c opt mssq
  • 如何分析组合的 python 和 c 代码

    我有一个由多个 python 脚本组成的应用程序 其中一些脚本正在调用 C 代码 该应用程序现在的运行速度比以前慢得多 因此我想对其进行分析以查看问题所在 是否有工具 软件包或只是一种分析此类应用程序的方法 有一个工具可以将 python
  • 在Python中连续解析文件

    我正在编写一个脚本 该脚本使用 HTTP 流量行解析文件 并取出域 目前仅将它们打印到屏幕上 我正在使用 httpry 将流量连续写入文件 这是我用来删除域名的脚本 usr bin python import re input open r
  • Pip 无法在 Windows 上安装 Twisted

    我正在尝试在 Windows 8 计算机上安装 Twisted 在 Twisted 官方网站上 只有一个 Windows 版的 Wheel 文件 https twistedmatrix com trac wiki Downloads htt
  • 如何在Tensorflow中保存估计器以供以后使用?

    我按照教程 TF Layers 指南 构建卷积神经网络 以下是代码 https github com tensorflow tensorflow blob r1 1 tensorflow examples tutorials layers
  • 在父类中访问子类变量

    我有一个父类和一个继承的子类 我想知道如何访问我的父类中的子类变量 我尝试了这个但失败了 class Parent object def init self print x class Child Parent x 1 x Child Er

随机推荐