一、PDF转Docx
1.准备Python第三方库
pip install pdf2docx
2.Python代码
import pdf2docxpdf_file = '将进酒.pdf'docx_file = '将进酒.docx'pdf2docx.parse(pdf_file, docx_file)
3.转换结果
pdf原图
docx转换结果
二、Docx转PDF
1.准备Python第三方库
pip install docx2pdf
2.Python代码
from docx2pdf import convertinput_path = '将进酒.docx'output_path = '将进酒pdf.pdf'convert(input_path, output_path) # 将 Word 文档转换为 PDF
3.转换结果
三、Docx转Doc、Doc转Docx
1.准备Python第三方库
pip install pypiwin32
2.Python代码
# docx为docfrom win32com import clientdocxFile = r'C:\Users\...\将进酒.docx'word = client.Dispatch('Word.Application') # 打开word应用程序docx = word.Documents.Open(docxFile) # 打开word文件docx.SaveAs(r'C:\Users\...\将进酒.doc', 0) # 另存为'.doc'的文件,参数0指doc文件docx.Close() # 关闭原来word文件word.Quit()
# doc转docxdocFile = r'C:\Users\...\将进酒.doc'word = client.Dispatch('Word.Application') # 打开word应用程序doc = word.Documents.Open(docFile) # 打开word文件doc.SaveAs(r'C:\Users\...\将进酒docx.docx', 12) # 另存为'.docx'文件,参数12或16指docx文件doc.Close() # 关闭原来word文件word.Quit()
3.转换结果
联系客服