1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from markitdown import MarkItDown
from win32com import client
 
office_id = 'kwps.Application'
 
 
def docx_to_markdown(doc_file: str) -> str:
    """
    将.docx文件转换为Markdown格式文本。
 
    该函数使用MarkItDown类的实例来处理传入的.docx文件,并将其内容转换为Markdown格式的文本。
    主要步骤包括创建MarkItDown类的实例、调用实例的convert方法处理文件,最后返回转换后的文本内容。
 
    参数:
    doc_file: str - .docx文件的路径,应包含文件名和扩展名。
 
    返回:
    str - 转换后的Markdown格式文本。
    """
    # 创建MarkItDown类的实例
    md = MarkItDown()
 
    # 使用MarkItDown实例转换.docx文件为Markdown格式文本
    result = md.convert(doc_file)
 
    # 返回转换后的文本内容
    return result.text_content
 
 
def doc_to_docx(doc_file: str, docx_file: str) -> None:
    """
    将.doc文件转换为.docx文件。
 
    参数:
    doc_file (str): 输入的.doc文件路径。
    docx_file (str): 输出的.docx文件路径。
 
    返回:
    None
    """
    try:
        word = client.Dispatch(office_id)
        doc = word.Documents.Open(doc_file)
        doc.SaveAs(docx_file, 12)  # 参数12表示保存为.docx格式
        doc.Close()
        word.Quit()
        print(f"文件 {doc_file} 已成功转换为 {docx_file}!")
    except Exception as e:
        print(f"出现错误: {e}")
 
 
def docx_to_pdf(docx_file: str, pdf_file: str) -> None:
    """
    将.docx文件转换为.pdf文件。
 
    参数:
    docx_file (str): 输入的.docx文件路径。
    pdf_file (str): 输出的.pdf文件路径。
 
    返回:
    None
    """
    try:
        word = client.Dispatch(office_id)
        doc = word.Documents.Open(docx_file)
        doc.SaveAs(pdf_file, 17)  # 17 表示保存为.pdf格式
        doc.Close()
        word.Quit()
        print(f"文件 {docx_file} 已成功转换为 {pdf_file}!")
    except  Exception as e:
        print(f"出现错误: {e}")
 
 
def test():
    # doc_to_docx("D:\\projects\\KnowledgeBase\\doc\\XA-5D无人机探测大纲(公开).doc",
    #             "D:\\projects\\KnowledgeBase\\doc\\XA-5D无人机探测大纲(公开)111.docx")
    # docx_to_pdf("D:/workspace/PythonProjects/KnowledgeBase/doc/ZL格式(公开).docx",
    #             "D:/workspace/PythonProjects/KnowledgeBase/doc/ZL格式(公开).pdf")
    import pymupdf4llm
    md_text = pymupdf4llm.to_markdown("D:/workspace/PythonProjects/KnowledgeBase/doc/ZL格式(公开).pdf")
    print(md_text)
 
 
if __name__ == '__main__':
    test()