doc.docx.pdf_如何在Linux上的命令行中将.docx .doc MS Word文件转换为pdf

doc.docx.pdf

How to convert a MS Word document file such as .docx and .doc to pdf on Linux using command line tools?

如何在Linux上使用命令行工具将MS Word文档文件(例如.docx和.doc)转换为pdf?

Use LibreOffice:

使用LibreOffice:

libreoffice --headless --convert-to pdf 
--outdir /path/to/out/dir/ /path/to/doc/docx/file
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。


While this LibreOffice answer works, the –headless option still needs as display, even though it does not use it. In otherwords, this option won’t work on a truely headless server or from a cron or batch job, unless you fire up a dummy X server (eg xvfb). The LibreOffice people don’t seem inclined to “fix” this:

尽管此LibreOffice答案有效,但–headless选项仍需要显示,即使它不使用它。 换句话说,除非您启动虚拟X服务器(例如xvfb),否则该选项在真正的无头服务器或cron或批处理作业中将不起作用。 LibreOffice人员似乎不倾向于“解决”此问题:

https://bugs.documentfoundation.org/show_bug.cgi?id=107151

https://bugs.documentfoundation.org/show_bug.cgi?id=107151



Hi Robert, thanks for the notes!

嗨罗伯特,谢谢你的笔记!

So in a server without a real X, it seems a command like this is needed (not yet tested):

因此,在没有实数X的服务器中,似乎需要这样的命令(尚未测试):

xvfb-run libreoffice --headless --convert-to pdf 
--outdir /path/to/out/dir/ /path/to/doc/docx/file

to make it run by cron or in background.

使它由cron或在后台运行。



TextMaker, the word processor included in SoftMaker FreeOffice, has a much better compatibility with Microsoft Word compared to LibreOffice. You should download FreeOffice without charge, open your doc/docx documents faithfully with TextMaker, press the pdf button, and get a perfect pdf file.

与LibreOffice相比,SoftMaker FreeOffice中包含的文字处理器TextMaker与Microsoft Word具有更好的兼容性。 您应该免费下载FreeOffice,使用TextMaker忠实地打开doc / docx文档,按pdf按钮,然后获得一个完美的pdf文件。

freeoffice.com

freeoffice.com



Does FreeOffice provides a command-line method to generate PDF from word .doc or .docx files? That is required to automatically generating many files.

FreeOffice是否提供了从Word .doc或.docx 文件生成PDF的命令行方法? 这是自动生成许多文件所必需的。

翻译自: https://www.systutorials.com/how-to-convert-a-docx-doc-ms-word-file-to-pdf-in-command-line-on-linux/

doc.docx.pdf

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是在 Python 中将 PDF 转换doc 的示例代码。 首先,你需要安装 Python 的第三方库「PyPDF2」和「python-docx」。你可以使用 pip 安装: ``` pip install pypdf2 pip install python-docx ``` 然后,你可以使用以下代码将 PDF 转换doc 文件: ``` import os import sys import shutil import pypdf2 from docx import Document # 读取 PDF 文件 with open('input.pdf', 'rb') as file: pdf = pypdf2.PdfFileReader(file) # 创建一个新的 Word 文档 document = Document() # 遍历 PDF 中的每一页 for page in range(pdf.getNumPages()): # 读取页面内容 page_content = pdf.getPage(page).extractText() # 将内容添加到 Word 文档中 document.add_paragraph(page_content) # 保存 Word 文档 document.save('output.docx') ``` 希望这能帮到你! ### 回答2: 以下是使用Python将PDF转换doc的代码示例: ```python import PyPDF2 def convert_pdf_to_doc(pdf_file_path, output_file_path): # 打开PDF文件 pdf_file = open(pdf_file_path, 'rb') # 创建一个PDF文件阅读器对象 pdf_reader = PyPDF2.PdfFileReader(pdf_file) # 创建一个Word文档对象 doc = PyPDF2.PdfFileWriter() # 将PDF的每一页内容逐页添加到Word文档中 for page_num in range(pdf_reader.numPages): page = pdf_reader.getPage(page_num) doc.addPage(page) # 将Word文档保存为doc文件 with open(output_file_path, 'wb') as doc_file: doc.write(doc_file) # 关闭文件 pdf_file.close() # 调用函数进行转换 pdf_file_path = 'input.pdf' # 输入PDF文件的路径 output_file_path = 'output.doc' # 输出doc文件的路径 convert_pdf_to_doc(pdf_file_path, output_file_path) ``` 代码的主要步骤如下: 1. 导入`PyPDF2`库。 2. 定义一个`convert_pdf_to_doc`函数,接收两个参数:PDF文件的路径和输出文档的路径。 3. 打开PDF文件,并创建一个PDF文件阅读器对象。 4. 创建一个Word文档对象。 5. 遍历PDF的每一页,将每一页内容逐页添加到Word文档中。 6. 将Word文档保存为doc文件。 7. 关闭文件。 8. 调用`convert_pdf_to_doc`函数进行转换,传入PDF文件的路径和输出文档的路径。 请注意,这里使用的是`PyPDF2`库,它不支持转换复杂的PDF文档,只能将简单的PDF文档转换doc文件。如果需要转换复杂的PDF文档,可能需要使用其他的库或工具。 ### 回答3: 要将PDF文件转换DOC文件,可以使用Python的第三方库"pdf2docx"来实现。下面是一个代码实例: ```python from pdf2docx import Converter def convert_pdf_to_doc(pdf_file, doc_file): # 创建转换器对象 converter = Converter(pdf_file) # 将PDF文件内容转换DOC格式 converter.convert(doc_file, start=0, end=None) # 关闭转换converter.close() # 指定要转换PDF文件转换后的DOC文件路径 pdf_file = "example.pdf" doc_file = "converted.docx" # 调用转换函数 convert_pdf_to_doc(pdf_file, doc_file) ``` 使用该代码,可以将名为"example.pdf"的PDF文件转换为名为"converted.docx"的DOC文件。需要注意的是,为了运行该代码,需要先安装"pdf2docx"库,可以使用以下命令进行安装: ``` pip install pdf2docx ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值