bash 通过命令行使用 LibreOffice 将 .xls 转换为 .pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25878987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert .xls to .pdf using LibreOffice via Command Line
提问by Rhenan Bartels
I'm trying to convert a .xls
file to .pdf
using LibreOffice via command line on Ubuntu. I have a kind of report on the .xls
file with some colors in the background of the cells and etc.
我正在尝试通过 Ubuntu 上的命令行将.xls
文件转换为.pdf
使用 LibreOffice。我有一种关于.xls
文件的报告,在单元格的背景中带有一些颜色等等。
The problem is when I convert the .xls
file, the .pdf
loses the original format. Each page is broken almost in the half and the content of one page is displayed in two different pages.
问题是当我转换.xls
文件时,.pdf
丢失了原始格式。每页几乎断了一半,一页的内容显示在两个不同的页面中。
- Does anybody know how to convert the
.xls
file to.pdf
via command line with keeping the original format? - Or some trick to set the size of the
.pdf
page to not break pages? (Also via command line)
- 有人知道如何通过命令行将
.xls
文件转换为.pdf
保持原始格式吗? - 或者一些技巧来设置
.pdf
页面的大小不打破页面?(也可以通过命令行)
The code I used to make the conversion was:
我用来进行转换的代码是:
soffice --headless --convert-to pdf:"impress_pdf_Export" filename.xls
回答by Kurt Pfeifle
If you use LibreOffice to convert Microsoft Excel (XLS) files to PDF documents, this is a two-step process (even if your command does look like it is a one-step process):
如果您使用 LibreOffice 将 Microsoft Excel (XLS) 文件转换为 PDF 文档,则这是一个两步过程(即使您的命令看起来确实是一个一步过程):
- Import the XLS into LibreOffice (even if started with
--headless
). - Export the PDF from LibreOffice.
- 将 XLS 导入 LibreOffice(即使以 开头
--headless
)。 - 从 LibreOffice 导出 PDF。
If the result does not look like you expect (not similar enough to Excel's native PDF export), then start with debugging the first step from above:
如果结果看起来不像您期望的(与 Excel 的原生 PDF 导出不够相似),则从上面的第一步开始调试:
Open the XLS file with LibreOffice in a GUI.Does it look like you expect it to look? Or are some formatting options looking weird?
Export the PDF from there (with the GUI).Are the page dimensions as you expect? Did you set them up how you prefer? The margins like you want them? etc.pp. ...
在 GUI 中使用 LibreOffice 打开 XLS 文件。它看起来像您期望的那样吗?或者一些格式选项看起来很奇怪?
从那里导出 PDF(使用 GUI)。页面尺寸是否符合您的预期?你有没有按照你喜欢的方式设置它们?你想要的利润率?等.pp ...
If you are working on Windows, you may also want to consider OfficeToPDF.exe. It is hosted on CodePlex, licensed with the Apache 2.0 License and available in binary and in source code.
如果您使用的是 Windows,您可能还需要考虑OfficeToPDF.exe。它托管在 CodePlex 上,获得 Apache 2.0 许可,并以二进制和源代码形式提供。
It requires a working Office 2013, Office 2010 orOffice 2007 installation. But then it can commandline- and batch-convert to PDF various MS Office-based file formats, including XLS(X), PPT(X), DOC(X), VSD(X) and PUB as well as Libre/OpenOffice-based ODT, ODS and ODC files.
它需要安装有效的 Office 2013、Office 2010或Office 2007。但随后它可以命令行和批量转换为 PDF 各种基于 MS Office 的文件格式,包括 XLS(X)、PPT(X)、DOC(X)、VSD(X) 和 PUB 以及基于 Libre/OpenOffice ODT、ODS 和 ODC 文件。
回答by einsty
Although this is a little bit off from the initial question (you don't _really need Office Libre if you have the Office suite and on a Windows machine)
尽管这与最初的问题有点不同(如果您拥有 Office 套件并且在 Windows 机器上,则实际上不需要 Office Libre)
I do appreciate the follow-up provided by Kurt. It prompted me to post the following Gist offering some clear instructions on how to go about using the .exe in a for loop.
我非常感谢 Kurt 提供的跟进。它促使我发布以下 Gist,提供有关如何在 for 循环中使用 .exe 的一些明确说明。