python使doc转换ppt

import os
import glob
from win32com.client import gencache

def get_doc_files():
# Specify the directory containing DOC files
file_path = ‘路径’ # Change this to your desired path
# Get all .doc and .docx files
doc_files = glob.glob(os.path.join(file_path, “.doc”))
return file_path, doc_files

def doc_to_pdf(filename, new_directory):
# Get the PDF filename
pdf_name = os.path.basename(filename).split(‘.doc’)[0] + ‘.pdf’
save_path_file = os.path.join(new_directory, pdf_name)

# Check if PDF already exists
if os.path.isfile(save_path_file):
    print(pdf_name, "converted already")
    return
    
# Initialize Word application
word = gencache.EnsureDispatch("Word.Application")
try:
    # Open the DOC file
    doc = word.Documents.Open(filename, False, False, False)
    # Save as PDF
    doc.ExportAsFixedFormat(save_path_file, 17)  # 17 represents PDF format
    doc.Close()
    print("Converted && saved:", save_path_file)
except Exception as e:
    print(os.path.split(filename)[1], "File conversion failed because", e)
finally:
    word.Quit()

def main():
# Get file paths
file_path, doc_files = get_doc_files()

# Create output directory
new_directory = os.path.join(file_path, "converted_pdfs")
if not os.path.exists(new_directory):
    os.mkdir(new_directory)

# Convert each DOC file
for doc_file in doc_files:
    doc_to_pdf(doc_file, new_directory)

if name == “main”:
main()
print(“Conversion completed!”)
input(“Press Enter to exit.”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值