import os
import time
from docx import Document
def method(html):
# 先处理成html 再写成word 进行处理
# 保存文件 先检查是否存在 上传后删除
htmlFilename = 'html-' + str(int(time.time())) + '.html'
htmlLocalUrl = './tmp/download/html/'
if os.path.exists(htmlLocalUrl):
# 打开文件用于写入
with open(htmlLocalUrl + htmlFilename, 'w') as file:
file.write(html)
else:
os.makedirs(htmlLocalUrl, exist_ok=True)
# 打开文件用于写入
with open(htmlLocalUrl + htmlFilename, 'w') as file:
file.write(html)
# html转word
wordFilename = 'docx-' + str(int(time.time())) + '.docx'
wordLocalUrl = './tmp/download/docx/'
try:
import pypandoc
if os.path.exists(wordLocalUrl):
pypandoc.convert_file(htmlLocalUrl + htmlFilename, 'docx', outputfile=wordLocalUrl + wordFilename)
else:
os.makedirs(wordLocalUrl, exist_ok=True)
pypandoc.convert_file(htmlLocalUrl + htmlFilename, 'docx', outputfile=wordLocalUrl + wordFilename)
except:
pass
# 删除html
if os.path.exists(htmlLocalUrl + htmlFilename):
os.remove(htmlLocalUrl + htmlFilename)
# 处理word
document = Document(wordLocalUrl + wordFilename)
# handle word table....
import time
from docx import Document
def method(html):
# 先处理成html 再写成word 进行处理
# 保存文件 先检查是否存在 上传后删除
htmlFilename = 'html-' + str(int(time.time())) + '.html'
htmlLocalUrl = './tmp/download/html/'
if os.path.exists(htmlLocalUrl):
# 打开文件用于写入
with open(htmlLocalUrl + htmlFilename, 'w') as file:
file.write(html)
else:
os.makedirs(htmlLocalUrl, exist_ok=True)
# 打开文件用于写入
with open(htmlLocalUrl + htmlFilename, 'w') as file:
file.write(html)
# html转word
wordFilename = 'docx-' + str(int(time.time())) + '.docx'
wordLocalUrl = './tmp/download/docx/'
try:
import pypandoc
if os.path.exists(wordLocalUrl):
pypandoc.convert_file(htmlLocalUrl + htmlFilename, 'docx', outputfile=wordLocalUrl + wordFilename)
else:
os.makedirs(wordLocalUrl, exist_ok=True)
pypandoc.convert_file(htmlLocalUrl + htmlFilename, 'docx', outputfile=wordLocalUrl + wordFilename)
except:
pass
# 删除html
if os.path.exists(htmlLocalUrl + htmlFilename):
os.remove(htmlLocalUrl + htmlFilename)
# 处理word
document = Document(wordLocalUrl + wordFilename)
# handle word table....