简体   繁体   English

如何使用 Python 将 docx 转换为 doc?

[英]How can I convert docx to doc using Python?

How can I convert file.docx to file.doc using Python?如何使用 Python 将 file.docx 转换为 file.doc? I have a code that outputs a file in docx format, but this program is for someone who can only use Word 2003, so I need to convert that file to.doc using Python.我有一个以 docx 格式输出文件的代码,但该程序适用于只能使用 Word 2003 的人,因此我需要使用 Python 将该文件转换为 .doc。 How can I do it?我该怎么做? Thank you in advance.先感谢您。

Bit clunky, but you could use pywinauto to programatically open your.docx documents in word, then save-as.doc.有点笨拙,但您可以使用 pywinauto 以编程方式以 word 格式打开 your.docx 文档,然后另存为.doc。 It'd be using word to do the conversion, so it should be as clean as you could get.它将使用 word 进行转换,因此它应该尽可能干净。

This is a snippet of what I've used for converting to pdf within word (it was just a test).这是我用来在 word 中转换为 pdf 的片段(这只是一个测试)。 You'd have to follow the keystrokes necessary to save as a.doc您必须按照保存为 a.doc 所需的按键操作

import pywinauto
from pywinauto.application import Application


app1 = Application(backend="uia").connect(path="C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\WINWORD.EXE")
wordhndl = app1.top_window()


wordhndl.type_keys('^o')
wordhndl.type_keys('%f')
wordhndl.type_keys('^o')
wordhndl.type_keys('^o')

#Now that we're in a sub-window, using the top_window() handle doesn't work...
#Instead refer to absolute (using friendly_class_name())
app1.Dialog.Open.type_keys("Y:\\996.Software\\04.Python\\Test\\SampleDoc1.docx")
app1.Dialog.Open.type_keys('~')


#Publish it to pdf
app1.SampleDoc1docx.type_keys('%f')
app1.SampleDoc1docx.type_keys('e')
app1.SampleDoc1docx.type_keys('p')
app1.SampleDoc1docx.type_keys('a')
app1.SampleDoc1docx.PublishasPDForXPS.Publish.type_keys('~')


#Deal with popups & prompts
if app1.Dialog.PublishasPDForXPS.ConfirmSaveAs.exists():
    app1.Dialog.PublishasPDForXPS.ConfirmSaveAs.Yes.click()  #This line can take some time...

I think the.doc keystrokes would be (not tested)我认为 .doc 击键将是(未测试)

app1.SampleDoc1docx.type_keys('%f')
app1.SampleDoc1docx.type_keys('a')
app1.SampleDoc1docx.type_keys('y')
app1.SampleDoc1docx.type_keys('4')
app1.SampleDoc1docx.type_keys('{DOWN}')
app1.SampleDoc1docx.type_keys('{DOWN}')
app1.SampleDoc1docx.type_keys('~')
app1.SampleDoc1docx.type_keys('{RIGHT}')
app1.SampleDoc1docx.type_keys('~')

But... the better solution is to use word.但是......更好的解决方案是使用word。 I've used VBA within word to do this exact thing before.我以前在 word 中使用过 VBA 来做这件事。 Don't have the code to hand, but a good pointer would be: https://www.datanumen.com/blogs/3-quick-ways-to-batch-convert-word-doc-to-docx-files-and-vice-versa/手头没有代码,但一个好的指针是: https://www.datanumen.com/blogs/3-quick-ways-to-batch-convert-word-doc-to-docx-files-反之亦然/

you can do something of the likes of:您可以执行以下操作:

import docx

doc = docx.Document("myWordxFile.docx")
doc.save('myNewWordFile.doc')

check this too.也检查一下 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM