Python判断Word文档格式(DOC/DOCX)
可以使用python-docx中的Package类来获取word文档的格式。具体代码如下:
from docx import Package
filename = 'your_file.docx'
# 打开word文档
doc = Package(filename)
# 判断文档格式
if doc.partname.endswith('.docx'):
print('This is a docx file.')
elif doc.partname.endswith('.doc'):
print('This is a doc file.')
else:
print('Unknown file format.')
解释一下代码:
- 首先,导入了python-docx中的Package类。
- 然后,指定要判断格式的word文档的文件名。
- 接着,使用Package类打开word文档。
- 最后,判断文档格式,如果文件名以'.docx'结尾,则是docx格式;如果以'.doc'结尾,则是doc格式。
原文地址: https://www.cveoy.top/t/topic/nChC 著作权归作者所有。请勿转载和采集!