【问题标题】:LibreOffice not maintaining the alignment after converting a docx to pdf using python使用 python 将 docx 转换为 pdf 后,LibreOffice 未保持对齐
【发布时间】:2022-08-17 19:21:22
【问题描述】:

我在我的 win 系统上安装了 Libreoffice,并且能够将 .docx 文件转换为 pdf

import os
import sys

newdir = os.path.abspath(os.path.join(os.path.dirname(__file__)))
if not os.path.exists(newdir):
    os.makedirs(newdir)

file_name = os.path.join(newdir, \'test.docx\')
print(file_name)
pdf_filename = file_name.split(\".docx\")[0] + \".pdf\"
pdf_file = os.path.join(pdf_filename)

from subprocess import Popen

if sys.platform == \'darwin\':
    LIBRE_OFFICE = \'/Applications/LibreOffice.app/Contents/MacOS/soffice\'
elif sys.platform == \'win32\':
    LIBRE_OFFICE = \'C:/Program Files/LibreOffice/program/soffice\'
else:
    LIBRE_OFFICE = \'libreoffice\'


def convert_to_pdf(input_docx, out_folder):
    p = Popen([LIBRE_OFFICE, \'--convert-to\', \'pdf\', \'--outdir\',
               out_folder, input_docx])
    print([LIBRE_OFFICE, \'--convert-to\', \'pdf\', input_docx])
    out = p.communicate()


sample_doc = file_name
out_folder = newdir
convert_to_pdf(sample_doc, out_folder)

pdf 未正确对齐,左右边距也丢失。 谁能告诉我们在将 docx 文件转换为 pdf 时是否可以改进或保持 libreoffice 中的对齐和样式?

  • 很大程度上取决于原始文件的格式质量。是的,您可以获得更好的结果。例如,您可以非常复杂的convert_to_pdf - 不要使用--convert-to,而是打开文件,设置所需的格式参数和export to PDF。另一种可能的解决方案是不使用 LibreOffice,而是使用 MS Word(也不能保证理想的结果)。
  • @k-j 我想让我的代码在所有平台上工作。这段代码是我们想要将 docx 转换为 pdf 的大项目的一部分。在本地,我们使用 win 和 mac 系统,但 dev/prod 服务器是基于 linux 的。大多数 python 的 docx 到 pdf 转换器模块只能在 win/mac 上工作,所以我选择了 libreoffice,但在这里我遇到了对齐问题。

标签: python python-3.x python-2.7 libreoffice libreoffice-basic


【解决方案1】:

您是否找到有关您的问题的任何解决方案。因为我现在面临同样的问题。似乎“LibreOffice”没有给出正确的结果

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 2018-12-01
    • 2018-10-02
    • 1970-01-01
    • 2015-03-06
    • 2021-06-17
    • 1970-01-01
    • 2016-11-16
    相关资源
    最近更新 更多