Python 运行时错误:未指定表列/单元格宽度,无法继续

Python 运行时错误:未指定表列/单元格宽度,无法继续,python,html,django,pdf,fpdf,Python,Html,Django,Pdf,Fpdf,我正试图在PythonDjango中使用pyfpdf创建一个PDF文件。下面的代码片段我正在尝试生成pdf格式的html代码 from fpdf import FPDF, HTMLMixin class WriteHtmlPDF(FPDF, HTMLMixin): pass html = f"""<h3>xxxxx</h3> <div style="border:1px solid #000"> <t

我正试图在PythonDjango中使用pyfpdf创建一个PDF文件。下面的代码片段我正在尝试生成pdf格式的html代码

    from fpdf import FPDF, HTMLMixin
    class WriteHtmlPDF(FPDF, HTMLMixin):
        pass

    html = f"""<h3>xxxxx</h3>
 <div style="border:1px solid #000">
     <table border="1" cellpadding="5" cellspacing="0" width="100%">
         <tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td>xxxxxxxxx</td></tr>
     </table>
 </div>
 <div>
     <table border="1" cellpadding="5" cellspacing="0" align="center" width=100>
         <tr>
             <td width=33>xxxxxx: 1</td>
             <td width=33>xxxxxx: 0</td>
             <td width=33>xxxxxx: 1</td>
         </tr>
     </table>
 </div>
 <table style="border: 1px solid black;" border="1" cellpadding="5" cellspacing="0" width=100>
     <thead align="center">
         <tr style="background-color: #6AB9F7;">
             <th>Sr No.</th>
             <th>Name</th>
             <th>xxxxx</th>
             <th>xxxxx</th>
             <th>xxxxx</th>
         </tr>
     </thead>
     <tbody align="center">
         <tr>
             <td>1</td>
             <td>xxxxxx</td>
             <td>xxxxxx</td>
             <td>xxxxxx</td>
             <td>xxxxxx</td>
         </tr>
     </tbody>
 </table>
"""
    pdf = WriteHtmlPDF()
    # First page
    pdf.add_page()
    pdf.write_html(html)
    pdf.output('html.pdf', 'F')

我尝试了解决方案,但仍然得到相同的错误

看起来
writehtmlpf()
不喜欢
标记

一个小小的抽搐修正:

from fpdf import FPDF, HTMLMixin
import os

class WriteHtmlPDF(FPDF, HTMLMixin):
    pass

pdf = WriteHtmlPDF()
# First page
pdf.add_page()

html = f"""<h3>xxxxx</h3>
 <div style="border:1px solid #000">
     <table border="1" cellpadding="5" cellspacing="0">
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
     </table>
 </div>
 <div>
     <table border="1" cellpadding="5" cellspacing="0">
         <tr>
             <td width="20">xxxxxx: 1</td>
             <td width="20">xxxxxx: 0</td>
             <td width="20">xxxxxx: 1</td>
         </tr>
     </table>
 </div>
 <table style="border: 1px solid black;" border="1" cellpadding="5" cellspacing="0">

         <tr style="background-color: #6AB9F7;">
             <td width="20">Sr. No</td>
             <td width="20">Name</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
         </tr>
         <tr>
             <td width="20">1</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
         </tr>
 </table>
"""

pdf.write_html(html)
fn = "html_tab.pdf"
pdf.output(fn, 'F')
从fpdf导入fpdf,HTMLMixin
导入操作系统
类WriteHtmlPDF(FPDF,HTMLMixin):
通过
pdf=WriteHtmlPDF()
#首页
pdf.add_page()
html=f“xxxxx”
xxxxxxxx:xxxxxxxx
xxxxxxxx:xxxxxxxxx
xxxxxxxx:xxxxxxxxx
xxxxxxxx:xxxxxxxxx
xxxxxx:1
xxxxxx:0
xxxxxx:1
高级工程师
名称
xxxxxx
xxxxxx
xxxxxx
1.
xxxxxx
xxxxxx
xxxxxx
xxxxxx
"""
pdf.write_html(html)
fn=“html\u tab.pdf”
pdf.输出(fn,'F')

给定的代码不会产生任何错误。请提供有帮助的代码段。@user5173426更新了问题并添加了代码段。如果下面的答案有帮助,您可以接受!干杯
from fpdf import FPDF, HTMLMixin
import os

class WriteHtmlPDF(FPDF, HTMLMixin):
    pass

pdf = WriteHtmlPDF()
# First page
pdf.add_page()

html = f"""<h3>xxxxx</h3>
 <div style="border:1px solid #000">
     <table border="1" cellpadding="5" cellspacing="0">
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
         <tr><th width=20 align="left">xxxxxxxx:</th><td width="100">xxxxxxxxx</td></tr>
     </table>
 </div>
 <div>
     <table border="1" cellpadding="5" cellspacing="0">
         <tr>
             <td width="20">xxxxxx: 1</td>
             <td width="20">xxxxxx: 0</td>
             <td width="20">xxxxxx: 1</td>
         </tr>
     </table>
 </div>
 <table style="border: 1px solid black;" border="1" cellpadding="5" cellspacing="0">

         <tr style="background-color: #6AB9F7;">
             <td width="20">Sr. No</td>
             <td width="20">Name</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
         </tr>
         <tr>
             <td width="20">1</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
             <td width="20">xxxxxx</td>
         </tr>
 </table>
"""

pdf.write_html(html)
fn = "html_tab.pdf"
pdf.output(fn, 'F')