Python & MS Word:将.doc转换为.docx?



我发现了几个与我的问题相似的问题,但是没有一个答案接近我的需求。

规格:我正在使用Python 3,没有MS Word。我的编程计算机正在运行OS X,并且Cloud Machine也是Linux/Ubuntu。

我正在使用python-docx从每晚发送给我的.doc文件中提取值。但是,python-docx仅与.docx文件一起使用,因此我需要先将文件转换为该扩展名。

所以,我有一个.doc文件,需要转换为.docx。这个脚本可能必须在云中运行,因此我无法安装任何类型的办公室或类似办公室的软件。可以做到吗?

您正在使用Linux/ubuntu,可以使用libreoffice的内置转换器。

语法

lowriter --convert-to docx *.doc

示例

lowriter --convert-to docx testdoc.doc

这将将所有DOC文件转换为DOCX并保存在同一文件夹中。

您可以使用unoconv-通用办公转换器。在Libreoffice/OpenOffice支持的任何文档格式之间转换。

unoconv -d document --format=docx *.doc
subprocess.call(['unoconv', '-d', 'document', '--format=docx', filename])

aspose.words for Python可以将DOC转换为DOCX。该软件包可以打开,生成,编辑,拆分,合并,比较,比较和转换任何平台上的python中的Word文档,而无需依赖MS Word。

这是一种付费产品,但免费计划提供150个免费的每月API电话。

P.S:我是Aspose的开发人员传播者。

# Import module
import asposewordscloud
import asposewordscloud.models.requests
from shutil import copyfile
# Get your credentials from https://dashboard.aspose.cloud (free registration is required).
words_api = asposewordscloud.WordsApi(app_sid='xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx',app_key='xxxxxxxxxxxxxxxxxxxxxxxxx')
words_api.api_client.configuration.host = 'https://api.aspose.cloud'
filename = 'C:/Temp/02_pages.doc'
dest_name = 'C:/Temp/02_pages.docx'
#Convert RTF to text
request = asposewordscloud.models.requests.ConvertDocumentRequest(document=open(filename, 'rb'), format='docx')
result = words_api.convert_document(request)
copyfile(result, dest_name)
import aspose.words as aw
path1="doc file path"
path2="path to save converted file"
file2=file.rsplit('.',1)[0]+'.docx'
filename1=os.path.join(path2,file2)
filename=os.path.join(path1,file)
doc = aw.Document(filename)
doc.save(filename1)

首先,您需要使用Windows。如果那是可接受的障碍,请继续阅读....

接下来,您需要安装Microsoft Office兼容性包。

现在下载并安装Microsoft Office迁移计划管理器。

要运行工具,您需要创建一个控制程序的.INI文件。一个示例。INI文件和更多信息,请访问此博客文章。这里有Microsoft的更多详细信息。

最新更新