Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the bullet appear directly next to the text of an indented list in the reportlab package for python?

I'm using reportlab 2.6's ListFlowable to make a bulleted list with colored circle bullets. However, I would like the bullet to appear next to the text, rather than aligned with the preceding, non-indented text. I tried to open up the ListFlowable source, but I couldn't find much there. Here's what I have:

from reportlab.platypus import Paragraph, ListFlowable, ListItem, SimpleDocTemplate, Frame
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.colors import CMYKColor

doc = SimpleDocTemplate("SOtest.pdf")
styles = getSampleStyleSheet()
Story = []
Story.append(Paragraph("Header Text, I dont want the bullets directly below the H"
                       ,styles['Normal']))
my_list = ListFlowable(
    [
        ListItem(Paragraph("Line 1",styles['Normal'])
                 ,bulletColor = CMYKColor(0.81, 0.45, 0.53, 0.23)
                 ,value = 'circle'
                 ),
        ListItem(Paragraph("Line 2",styles['Normal'])
                 ,bulletColor = CMYKColor(0.81, 0.45, 0.53, 0.23)
                 ,value = 'circle'
                 )
        ],
    bulletType='bullet',
    start='circle'
    )

Story.append(my_list)
doc.build(Story)

This code results in this: Not Desired

But I want it to look like: Desired

I manually edited the second image to get the desired effect.

I thought about making a list inside a list, to get an indented bullet, but then I wouldn't know how to dedent the text closer to the bullet.

like image 294
Kyle Heuton Avatar asked Feb 21 '13 07:02

Kyle Heuton


2 Answers

Just pass a leftIndent parameter into the ListItem.

my_list = ListFlowable([
    ListItem(Paragraph("Line 1", styles['Normal']),
         leftIndent=35, value='circle',
         bulletColor=CMYKColor(0.81, 0.45, 0.53, 0.23)
    ),
    ListItem(Paragraph("Line 2", styles['Normal']),
         leftIndent=35, value='circle',
         bulletColor=CMYKColor(0.81, 0.45, 0.53, 0.23))
],
bulletType='bullet',
start='circle',
leftIndent=10
)

EDIT: You have to set the leftIndent of the ListFlowable for defining the space between the bullets and the text.

like image 147
floqqi Avatar answered Oct 31 '22 16:10

floqqi


I ended up looking further into it (the ReportLab source code…) to discover how to deal with the bullets not being correctly-aligned vertically, either. I suggest the following additional kwargs for other bulleted-list control freaks:

  • bulletOffsetY +ve to move it up, -ve to move it down
  • bulletFontSize if you ended up with huge bullets by default like I did
  • spaceBefore and spaceAfter to control the vertical gaps between list items
like image 43
supervacuo Avatar answered Oct 31 '22 16:10

supervacuo