{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from spire.doc import *\n",
    "from spire.doc.common import *\n",
    " \n",
    "# 创建一个 Document 对象\n",
    "document = Document()\n",
    "# 加载一个 HTML 文件\n",
    "document.LoadFromFile(\"E:/tem/test/2.html\", FileFormat.Html, XHTMLValidationType.Transitional)\n",
    " \n",
    "# 将 HTML 文件保存为 DOCX 文件\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SpireException",
     "evalue": "IO_SharingViolation_File, e:\\project\\python\\工具\\Html转Docx.docx:   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String, FileMode, FileAccess, FileShare, FileOptions) + 0x15c\r\n   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String, FileMode, FileAccess, FileShare, FileOptions, Int64, Nullable`1) + 0x95\r\n   at System.IO.Strategies.OSFileStreamStrategy..ctor(String, FileMode, FileAccess, FileShare, FileOptions, Int64, Nullable`1) + 0x50\r\n   at sprfa9.spra(Document, String) + 0xaf\r\n   at Spire.Doc.Document.sprq(String) + 0x59\r\n   at Spire.Doc.AOT.NLDocument.Document_SaveToFileFF(IntPtr, IntPtr, Int32, IntPtr) + 0x6d",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mSpireException\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m document\u001b[38;5;241m.\u001b[39mSaveToFile(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHtml转Docx.docx\u001b[39m\u001b[38;5;124m\"\u001b[39m, FileFormat\u001b[38;5;241m.\u001b[39mDocx2016)\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# 或将结果文档保存为 DOC 文件\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# document.SaveToFile(\"Html转Doc.doc\", FileFormat.Doc)\u001b[39;00m\n\u001b[0;32m      5\u001b[0m document\u001b[38;5;241m.\u001b[39mClose()\n",
      "File \u001b[1;32md:\\Anaconda3\\Lib\\site-packages\\plum\\function.py:642\u001b[0m, in \u001b[0;36m_BoundFunction.__call__\u001b[1;34m(self, *args, **kw_args)\u001b[0m\n\u001b[0;32m    641\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkw_args):\n\u001b[1;32m--> 642\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mf(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minstance, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkw_args)\n",
      "File \u001b[1;32md:\\Anaconda3\\Lib\\site-packages\\plum\\function.py:592\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[1;34m(self, *args, **kw_args)\u001b[0m\n\u001b[0;32m    590\u001b[0m \u001b[38;5;66;03m# Cache failed. Get method and return type, and perform call.\u001b[39;00m\n\u001b[0;32m    591\u001b[0m method, return_type \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mresolve_method(\u001b[38;5;241m*\u001b[39msig_types)\n\u001b[1;32m--> 592\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert(method(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkw_args), return_type)\n",
      "File \u001b[1;32md:\\Anaconda3\\Lib\\site-packages\\spire\\doc\\Document.py:2492\u001b[0m, in \u001b[0;36mDocument.SaveToFile\u001b[1;34m(self, fileName, fileFormat)\u001b[0m\n\u001b[0;32m   2489\u001b[0m enumfileFormat:c_int \u001b[38;5;241m=\u001b[39m fileFormat\u001b[38;5;241m.\u001b[39mvalue\n\u001b[0;32m   2491\u001b[0m GetDllLibDoc()\u001b[38;5;241m.\u001b[39mDocument_SaveToFileFF\u001b[38;5;241m.\u001b[39margtypes\u001b[38;5;241m=\u001b[39m[c_void_p ,c_char_p,c_int]\n\u001b[1;32m-> 2492\u001b[0m CallCFunction(GetDllLibDoc()\u001b[38;5;241m.\u001b[39mDocument_SaveToFileFF,\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mPtr, fileNamePtr,enumfileFormat)\n",
      "File \u001b[1;32md:\\Anaconda3\\Lib\\site-packages\\spire\\doc\\common\\__init__.py:129\u001b[0m, in \u001b[0;36mCallCFunction\u001b[1;34m(func, *args, **kwargs)\u001b[0m\n\u001b[0;32m    127\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m old_value \u001b[38;5;241m!=\u001b[39m modified_value:\n\u001b[0;32m    128\u001b[0m     info \u001b[38;5;241m=\u001b[39m PtrToStr(modified_value)\n\u001b[1;32m--> 129\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m SpireException(info)\n\u001b[0;32m    130\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
      "\u001b[1;31mSpireException\u001b[0m: IO_SharingViolation_File, e:\\project\\python\\工具\\Html转Docx.docx:   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String, FileMode, FileAccess, FileShare, FileOptions) + 0x15c\r\n   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String, FileMode, FileAccess, FileShare, FileOptions, Int64, Nullable`1) + 0x95\r\n   at System.IO.Strategies.OSFileStreamStrategy..ctor(String, FileMode, FileAccess, FileShare, FileOptions, Int64, Nullable`1) + 0x50\r\n   at sprfa9.spra(Document, String) + 0xaf\r\n   at Spire.Doc.Document.sprq(String) + 0x59\r\n   at Spire.Doc.AOT.NLDocument.Document_SaveToFileFF(IntPtr, IntPtr, Int32, IntPtr) + 0x6d"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
      "\u001b[1;31m请查看单元格中的代码，以确定故障的可能原因。\n",
      "\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
      "\u001b[1;31m有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "document.SaveToFile(\"Html转Docx.docx\", FileFormat.Docx2016)\n",
    "# 或将结果文档保存为 DOC 文件\n",
    "# document.SaveToFile(\"Html转Doc.doc\", FileFormat.Doc)\n",
    " \n",
    "document.Close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "document.Close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "No pandoc was found: either install pandoc and add it\nto your PATH or or call pypandoc.download_pandoc(...) or\ninstall pypandoc wheels with included pandoc.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 9\u001b[0m\n\u001b[0;32m      6\u001b[0m     html_text \u001b[38;5;241m=\u001b[39m f\u001b[38;5;241m.\u001b[39mread()\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m# 使用pypandoc将HTML转换成Word格式\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m output \u001b[38;5;241m=\u001b[39m pypandoc\u001b[38;5;241m.\u001b[39mconvert_text(html_text, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdocx\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mformat\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhtml\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m     11\u001b[0m \u001b[38;5;66;03m# 创建一个新的Word文档\u001b[39;00m\n\u001b[0;32m     12\u001b[0m doc \u001b[38;5;241m=\u001b[39m Document()\n",
      "File \u001b[1;32md:\\Anaconda3\\Lib\\site-packages\\pypandoc\\__init__.py:94\u001b[0m, in \u001b[0;36mconvert_text\u001b[1;34m(source, to, format, extra_args, encoding, outputfile, filters, verify_format, sandbox, cworkdir)\u001b[0m\n\u001b[0;32m     58\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Converts given `source` from `format` to `to`.\u001b[39;00m\n\u001b[0;32m     59\u001b[0m \n\u001b[0;32m     60\u001b[0m \u001b[38;5;124;03m:param str source: Unicode string or bytes (see encoding)\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     91\u001b[0m \u001b[38;5;124;03m        path.\u001b[39;00m\n\u001b[0;32m     92\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m     93\u001b[0m source \u001b[38;5;241m=\u001b[39m _as_unicode(source, encoding)\n\u001b[1;32m---> 94\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_input(source, \u001b[38;5;28mformat\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mstring\u001b[39m\u001b[38;5;124m'\u001b[39m, to, extra_args\u001b[38;5;241m=\u001b[39mextra_args,\n\u001b[0;32m     95\u001b[0m                       outputfile\u001b[38;5;241m=\u001b[39moutputfile, filters\u001b[38;5;241m=\u001b[39mfilters,\n\u001b[0;32m     96\u001b[0m                       verify_format\u001b[38;5;241m=\u001b[39mverify_format, sandbox\u001b[38;5;241m=\u001b[39msandbox,\n\u001b[0;32m     97\u001b[0m                       cworkdir\u001b[38;5;241m=\u001b[39mcworkdir)\n",
      "File \u001b[1;32md:\\Anaconda3\\Lib\\site-packages\\pypandoc\\__init__.py:367\u001b[0m, in \u001b[0;36m_convert_input\u001b[1;34m(source, format, input_type, to, extra_args, outputfile, filters, verify_format, sandbox, cworkdir, sort_files)\u001b[0m\n\u001b[0;32m    364\u001b[0m _check_log_handler()\n\u001b[0;32m    366\u001b[0m logger\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnsuring pandoc path...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m--> 367\u001b[0m _ensure_pandoc_path()\n\u001b[0;32m    369\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m verify_format:\n\u001b[0;32m    370\u001b[0m     logger\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mVerifying format...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\Anaconda3\\Lib\\site-packages\\pypandoc\\__init__.py:802\u001b[0m, in \u001b[0;36m_ensure_pandoc_path\u001b[1;34m()\u001b[0m\n\u001b[0;32m    794\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(textwrap\u001b[38;5;241m.\u001b[39mdedent(\u001b[38;5;124m\"\"\"\u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[0;32m    795\u001b[0m \u001b[38;5;124m    See http://johnmacfarlane.net/pandoc/installing.html\u001b[39m\n\u001b[0;32m    796\u001b[0m \u001b[38;5;124m    for installation options\u001b[39m\n\u001b[0;32m    797\u001b[0m \u001b[38;5;124m\u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m))\n\u001b[0;32m    798\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(textwrap\u001b[38;5;241m.\u001b[39mdedent(\u001b[38;5;124m\"\"\"\u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[0;32m    799\u001b[0m \u001b[38;5;124m    ---------------------------------------------------------------\u001b[39m\n\u001b[0;32m    800\u001b[0m \n\u001b[0;32m    801\u001b[0m \u001b[38;5;124m\u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m))\n\u001b[1;32m--> 802\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo pandoc was found: either install pandoc and add it\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    803\u001b[0m               \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto your PATH or or call pypandoc.download_pandoc(...) or\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    804\u001b[0m               \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minstall pypandoc wheels with included pandoc.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mOSError\u001b[0m: No pandoc was found: either install pandoc and add it\nto your PATH or or call pypandoc.download_pandoc(...) or\ninstall pypandoc wheels with included pandoc."
     ]
    }
   ],
   "source": [
    "from docx import Document\n",
    "import pypandoc\n",
    " \n",
    "# 读取HTML文件\n",
    "with open(\"1.html\", \"r\") as f:\n",
    "    html_text = f.read()\n",
    " \n",
    "# 使用pypandoc将HTML转换成Word格式\n",
    "output = pypandoc.convert_text(html_text, 'docx', format='html')\n",
    " \n",
    "# 创建一个新的Word文档\n",
    "doc = Document()\n",
    " \n",
    "# 将pypandoc转换的Word内容添加到文档中\n",
    "doc.add_paragraph(output)\n",
    " \n",
    "# 保存文档\n",
    "doc.save(\"output.docx\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
