我在MS Excel中创建了一个模板文件,导出为html。模板文件包含多个字段,需要使用交互式PyQt4表单应用程序填充参数。问题是PyQt4 webview正确呈现文件,只有当有单个html文件时才能直接打印,但是对于多个文件(即生成的css和xml文件),它不会返回正确的输出。没有css和xml文件,布局和样式不匹配,并产生乱码打印。 代码是
dpi = 50
printer = QtGui.QPrinter()
printer.setResolution(dpi)
printer.setPageSize(QPrinter.A4)
printer.setPageMargins(12, 20, 10, 5, QPrinter.Millimeter)
document = QtWebKit.QWebView(self)
document.setHtml(self.template_html
,QUrl("./misc/")) // here "misc" folder contains another
// folder "template_html_files" containing
// the css and xml file
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("added.pdf")
self.template_html包含主要生成页面的html标签(使用open
手动加载),以下文件由MS excel生成,包括
filelist.xml
sheet001.htm
stylesheet.css
tabstrip.htm
目录结构是
./
|
|-----MainApplication.py
|-----misc
|
|-----template_html.html
|-----template_html_files
|
|-------filelist.xml
|-------sheet001.htm
|-------stylesheet.css
|-------tabstrip.htm
答案 0 :(得分:1)
QWebView,setHtml()
的第二个参数必须是QUrl
。我认为,QUrl
不能是路径字符串,它必须是网址,请参阅
Qt Documentation QUrl
最简单的方法是直接使用html文件:
self.load(QtCore.QUrl('file://path to file')
发现中的
<LINK type="text/css" rel="stylesheet" href="./misc/Formatierung.CSS">
我只使用了html文件的绝对路径