Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
fpdf,如何在python中断行?_Python_Fpdf - Fatal编程技术网

fpdf,如何在python中断行?

fpdf,如何在python中断行?,python,fpdf,Python,Fpdf,我正在pdf中创建几个表。 由于一些bug,我无法用html编写代码,所以我只使用python import fpdf from fpdf import FPDF def create_table(data, padding): # Create instance of FPDF class # Letter size paper, use inches as unit of measure pdf=FPDF(format='letter', unit='in')

我正在pdf中创建几个表。 由于一些bug,我无法用html编写代码,所以我只使用python

import fpdf
from fpdf import FPDF

def create_table(data, padding):
    # Create instance of FPDF class
    # Letter size paper, use inches as unit of measure
    pdf=FPDF(format='letter', unit='in')
    # Add new page. Without this you cannot create the document.
    pdf.add_page()
    # Remember to always put one of these at least once.
    pdf.set_font('Times','',10.0) 
    # Effective page width, or just epw
    epw = pdf.w - 2*pdf.l_margin
    for table in data:
        # Set column width to 1/4 of effective page width to distribute content 
        # evenly across table and page
        col_width = epw/len(table[1][0])
        # Document title centered, 'B'old, 14 pt
        pdf.set_font('Times','B',14.0) 
        pdf.cell(epw, 0.0, table[0][0], align='C')
        pdf.set_font('Times','',10.0) 
        pdf.ln(0.5)
        th = pdf.font_size
        # Here we add more padding by passing 2*th as height
        for row in table[1]:
            for datum in row:
                # Enter data in colums
                pdf.cell(col_width, padding*th, str(datum), border=1)
            pdf.ln(padding*th)
        # Line break equivalent to 4 lines
        pdf.ln(4*th)

    pdf.output('table-using-cell-borders.pdf','F')


table_1 = [
['table_1'],
#headers
[['A', 'Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccb'],
#values
['a1', 'b1'],
['a2', 'b2'],
]]

table_2 = [
['table_2'],
#headers
[['C', 'D'],
#values
['a1', 'b1'],
['a2', 'b2'],
]]

create_table([table_1, table_2], 3)
如您所见,这是ouptut: 在表_1中,字符串不会中断到下一行, “\n”也不起作用


我可以使用什么代码来断开表1中的字符串?

您是否尝试了multi-cell?\n谢谢,找到了正确的代码。请把它作为一个答案,这样我就可以给你+10哈哈,没什么大不了的朋友。很高兴你找到了你的答案。