|
|
import os |
|
|
import time |
|
|
|
|
|
import win32com.client as win32 |
|
|
from win32com.client import constants |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_doc_to_docx(doc_path, docx_path=None): |
|
|
""" |
|
|
Convert a .doc file to .docx format. |
|
|
|
|
|
Args: |
|
|
doc_path (str): Path to the input .doc file |
|
|
docx_path (str, optional): Path to save the output .docx file. |
|
|
If None, saves to same directory with .docx extension. |
|
|
Returns: |
|
|
str: Path to the converted file |
|
|
""" |
|
|
|
|
|
if docx_path is None: |
|
|
base = os.path.splitext(doc_path)[0] |
|
|
docx_path = base + ".docx" |
|
|
|
|
|
|
|
|
doc_path = os.path.abspath(doc_path) |
|
|
docx_path = os.path.abspath(docx_path) |
|
|
|
|
|
try: |
|
|
|
|
|
word = win32.gencache.EnsureDispatch("Word.Application") |
|
|
word.Visible = False |
|
|
|
|
|
|
|
|
doc = word.Documents.Open(doc_path) |
|
|
|
|
|
|
|
|
doc.SaveAs(docx_path, FileFormat=constants.wdFormatXMLDocument) |
|
|
|
|
|
|
|
|
doc.Close(False) |
|
|
word.Quit() |
|
|
|
|
|
print(f"Successfully converted: {doc_path} -> {docx_path}") |
|
|
return docx_path |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error converting {doc_path}: {str(e)}") |
|
|
return None |
|
|
|
|
|
|
|
|
def convert_folder(input_folder, output_folder=None): |
|
|
""" |
|
|
Convert all DOC files in a folder to DOCX format. |
|
|
|
|
|
Args: |
|
|
input_folder (str): Path to folder containing .doc files |
|
|
output_folder (str, optional): Folder to save converted files. |
|
|
If None, saves to same folder as input. |
|
|
""" |
|
|
|
|
|
if not os.path.isdir(input_folder): |
|
|
print(f"Error: Input folder does not exist: {input_folder}") |
|
|
return |
|
|
|
|
|
|
|
|
if output_folder and not os.path.exists(output_folder): |
|
|
os.makedirs(output_folder) |
|
|
|
|
|
|
|
|
doc_files = [ |
|
|
f |
|
|
for f in os.listdir(input_folder) |
|
|
if f.lower().endswith(".doc") and os.path.isfile(os.path.join(input_folder, f)) |
|
|
] |
|
|
|
|
|
if not doc_files: |
|
|
print("No DOC files found in the input folder.") |
|
|
return |
|
|
|
|
|
print(f"Found {len(doc_files)} DOC files to convert.") |
|
|
|
|
|
|
|
|
success_count = 0 |
|
|
for doc_file in doc_files: |
|
|
input_path = os.path.join(input_folder, doc_file) |
|
|
|
|
|
if output_folder: |
|
|
output_path = os.path.join( |
|
|
output_folder, os.path.splitext(doc_file)[0] + ".docx" |
|
|
) |
|
|
|
|
|
else: |
|
|
output_path = None |
|
|
|
|
|
if convert_doc_to_docx(input_path, output_path): |
|
|
success_count += 1 |
|
|
|
|
|
time.sleep(1) |
|
|
|
|
|
print( |
|
|
f"\nConversion complete. Successfully converted {success_count} of {len(doc_files)} files." |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
input_folder = ( |
|
|
r"C:\Users\David\Downloads\TR_ FN4B" |
|
|
) |
|
|
output_folder = r"C:\Users\David\Downloads\TR_ FN4B\Nouveau dossier" |
|
|
|
|
|
convert_folder(input_folder, output_folder) |
|
|
|