我正在尝试使用soffice &python将.ods文件转换为pdf:
import os
import subprocess
def ods_to_pdf(ods_filename):
file = os.path.join(os.getcwd(), ods_filename)
path_to_soffice = "path/to/soffice"
subprocess.run([path_to_soffice, "--headless", "--convert-to", "pdf", file], check=True)它工作得很好,但生成的pdf最后有一个空白页(有时是两个)。有人知道我怎样才能防止这种行为吗?代码运行在Docker容器中,使用Ubuntu 18.04作为基础镜像。LibreOffice版本: 7.1.0 (我也尝试过6.1.6.3,结果相同)。
发布于 2021-02-19 20:19:11
我不知道如何阻止LibreOffice添加空白页,但通过在转换后删除空白页解决了这个问题:
import PyPDF2
output = PyPDF2.PdfFileWriter()
input = PyPDF2.PdfFileReader(open("file.pdf", "rb"))
number_of_pages = input.getNumPages()
for current_page_number in range(number_of_pages):
page = input.getPage(current_page_number)
if page.extractText() != "":
output.addPage(page)
output_stream = open("output.pdf", "wb")
output.write(output_stream)https://stackoverflow.com/questions/66273978
复制相似问题