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.”)