import pypandoc
output = pypandoc.convert_file('file.html', 'docx', outputfile="file1.docx")
assert output == ""
它正在生成新的docx文件,但忽略了样式。
有人可以告诉我如何生成带有样式的新docx文件吗?
提前感谢您的回答。
import pypandoc
output = pypandoc.convert_file('file.html', 'docx', outputfile="file1.docx")
assert output == ""
它正在生成新的docx文件,但忽略了样式。
有人可以告诉我如何生成带有样式的新docx文件吗?
提前感谢您的回答。
extra_args='--css=custom_file.css'
来包含 CSS 文件。您可以使用静态文件或使用模板生成临时文件来完成此操作。 - Emin Mastizada--css
参数在转换为 HTML 时使用,但在从 HTML 转换时被忽略了。或者我有什么遗漏吗? - Matthew Strawbridgefrom htmldocx import HtmlToDocx
new_parser = HtmlToDocx()
new_parser.parse_html_file("html_filename", "docx_filename")
#Files extensions not needed, but tolerated
from django.shortcuts import render
from django.http import HttpResponse
from io import BytesIO
from html2docx import html2docx
def generate_docx(request):
context = {
'data': 'Hello, this is your data!',
}
html_content_bytes = render(request, 'your_template.html',
context).content
html_content_str = html_content_bytes.decode('utf-8')
output = BytesIO()
byte_data = html2docx(html_content_str, output)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename=output.docx'
response.write(byte_data.getvalue())
return response