用pypand将html表转换为docx文件

2024-05-26 16:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

Pandoc不能很好地将HTML表转换成docx文档。我得到一个请求的内容,我使用一个模板文件呈现它。然后我就这样用pypandoc:

 response = render(                                     
   request,                                      
   'template.html',                      
   {                                             
     "field1": f1,                               
     "field1": f2,     
   }                                             
 )                                               

 import pypandoc                                                                                            
 pypandoc.convert(source=response.content, format='html', to='docx', outputfile='output.docx')  

在模板.html包含一个表。在docx文件中,我得到一个表,其内容在下面分开。有没有额外的参数要考虑来解决这个问题?或者pandoc转换还不支持well表?有什么功能性的例子吗?也许有更简单的方法吗?在


编辑1

我提供了更简洁的例子。下面是一个测试python代码片段:

^{pr2}$

我打开html文件:

$ firefox test-table.html 

并获取以下html页面:

enter image description here

这很好。我还得到了以下docx文档:

$ libreoffice test-table.docx 

enter image description here

这不好。在

我将docx文件导出为pdf文件,并得到以下输出:

$ evince test-table.pdf 

enter image description here

请注意,我们在图片中看到的是整个页面,没有滚动的可能。第二列的日期根本不存在。有什么想法吗?在


编辑2

Pandoc已安装在conda环境中:

$ type pandoc
pandoc is hashed (/home/kaligne/local/miniconda3/bin/pandoc)

Pandoc版本是:

$ pandoc -v
pandoc 2.2.1
Compiled with pandoc-types 1.17.4.2, texmath 0.11, skylighting 0.7.0.2
Default user data directory: /home/kaligne/.pandoc
Copyright (C) 2006-2018 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

编辑3 我将docx文件转换为txt:

$ docx2txt test-table.docx
$ cat test-table.txt 
Table with colgroup and col
Lime
Lemon
Green
Yellow
Fruit
Fruit

我们可以看到所有的数据都在。所以我想这和信息的显示方式有关。在


Tags: 文件文档test模板编辑内容foris

热门问题