一、docx轉html bat
通過寫一個bat腳本實現docx文件的批量轉換為html文件,代碼示例如下:
@echo off
setlocal EnableDelayedExpansion
for /r %%i in (*.docx) do (
set ename=%%~ni
set ename=!ename:~0,-4!
"C:\Program Files\Microsoft Office\root\Office16\Wordconv.exe" -oice -nme "%%i" -out "%%~pi!ename!.html" -shtml
)
pause
二、docx轉word
如果需要在docx和html之間進行轉換,可以先將docx轉換為word,再將word轉換為html,代碼示例如下:
from win32com import client
import os
def docx_to_word(docx_path, word_path):
word = client.Dispatch('Word.Application')
docx = word.Documents.Open(docx_path)
docx.SaveAs(word_path, 16)
docx.Close()
word.Quit()
def word_to_html(word_path, html_path):
word = client.Dispatch('Word.Application')
html_doc = word.Documents.Add(word_path)
html_doc.SaveAs2(html_path, 8)
html_doc.Close()
word.Quit()
docx_to_word("example.docx", "example.doc")
word_to_html("example.doc", "example.html")
三、docx轉html亂碼
在轉換docx為html時,有可能會出現中文亂碼的問題,需要進行編碼轉換,代碼示例如下:
import mammoth
with open("example.docx", "rb") as docx_file:
result = mammoth.convert_to_html(docx_file, convert_image = mammoth.images.img_element(convert = mammoth.images.relative_to_document("example.docx")))
with open("example.html", "w", encoding = "utf-8") as html_file:
html_file.write(result.value)
四、docx轉為txt
將docx轉換為txt文件可以使用Python-docx庫,在轉換之前需要安裝Python-docx庫,代碼示例如下:
from docx import Document
def docx_to_txt(docx_path, txt_path):
document = Document(docx_path)
with open(txt_path, "w", encoding = "utf-8") as txt_file:
for para in document.paragraphs:
txt_file.write(para.text + "\n")
docx_to_txt("example.docx", "example.txt")
五、docx轉excel
將docx轉換為excel文件可以使用Python-docx庫和openpyxl庫,在轉換之前需要安裝這兩個庫,代碼示例如下:
from docx import Document
from openpyxl import Workbook
def docx_to_excel(docx_path, excel_path):
document = Document(docx_path)
wb = Workbook()
ws = wb.active
for table in document.tables:
for i, row in enumerate(table.rows):
for j, cell in enumerate(row.cells):
ws.cell(row = i + 1, column = j + 1, value = cell.text)
wb.save(excel_path)
docx_to_excel("example.docx", "example.xlsx")
六、轉docx文檔
如果需要將html轉換為docx文檔,可以使用Python-docx庫,代碼示例如下:
from docx import Document
from docx.shared import Inches
document = Document()
with open("example.html", "r", encoding = "utf-8") as html_file:
content = html_file.read()
document.add_paragraph(content)
document.add_picture("example.png", width = Inches(6))
document.save("example.docx")
七、docx轉為doc格式
如果需要將docx轉換為doc格式,可以使用Python-docx庫和win32com庫,代碼示例如下:
from docx import Document
from win32com import client
def docx_to_doc(docx_path, doc_path):
document = Document(docx_path)
document.save(doc_path)
word = client.Dispatch("Word.Application")
doc = word.Documents.Open(doc_path)
doc.SaveAs2(doc_path[:-1], 0)
doc.Close()
word.Quit()
docx_to_doc("example.docx", "example.doc")
八、docx轉txt
將docx轉換為txt文件可以使用Python-docx庫,在轉換之前需要安裝Python-docx庫,代碼示例如下:
from docx import Document
def docx_to_txt(docx_path, txt_path):
document = Document(docx_path)
with open(txt_path, "w", encoding = "utf-8") as txt_file:
for para in document.paragraphs:
txt_file.write(para.text + "\n")
docx_to_txt("example.docx", "example.txt")
九、docx轉doc
如果需要將docx轉換為doc格式,可以使用Python-docx庫和win32com庫,代碼示例如下:
from docx import Document
from win32com import client
def docx_to_doc(docx_path, doc_path):
document = Document(docx_path)
document.save(doc_path)
docx_to_doc("example.docx", "example.doc")
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/237730.html