1

我正在使用 ReportLab 和 Python 2.7 排版一个大型文档(>1,000 页)。我计划将它排版为每页两列,并且已经设法或多或少地工作。

def frameMargins(doc, nc, id, cs = 5 * mm):
""" Calculates the frame margins for the page-geometry specified in doc
if it is to have nc columns with a column separator of cs between each.
Return value is two lists (odd and even) with nc sets of dimensions in each """

odd =  []
even = []

# First work out the column-width

bo = BINDINGOFFSET
pw = doc.pagesize[0]
ph = doc.pagesize[1]
lm = doc.leftMargin
rm = doc.rightMargin
bm = doc.bottomMargin
tm = doc.topMargin
dh = doc.height

cw = (pw - lm - bo - rm - cs * (nc - 1))/float(nc)

for c in range(nc):
    odd_left =  bo + lm + c * (cw + cs)
    odd_right = cw
    odd.append(Frame(odd_left, bm, odd_right, dh, id="{}-oc-{}".format(id.replace(" ", "").lower(), c)))
    even_left = lm + c * (cw + cs)
    even_right = cw
    even.append(Frame(even_left, bm, even_right, dh, id="{}-ec-{}".format(id.replace(" ", "").lower(), c)))
return odd, even

def WritePDF(files):

story = []
styles = getSampleStyleSheet()

doc = BaseDocTemplate("PolariBible.pdf", pagesize=A4, title = "Polari Bible 5th Edition",
                      leftMargin = 25 * mm, rightMargin = 25 * mm, 
                      topMargin = 25 * mm, bottomMargin = 25 * mm)

colOddFrames, colEvenFrames = frameMargins(doc, COLUMNS, "col")
titleOddFrames, titleEvenFrames = frameMargins(doc, 1, "title")

doc.addPageTemplates([PageTemplate(id='OddTitle',  frames=titleOddFrames,  onPage=oddFooter),
                      PageTemplate(id='EvenTitle', frames=titleEvenFrames, onPage=evenFooter),
                      PageTemplate(id='OddCols',   frames=colOddFrames,    onPage=oddFooter),
                      PageTemplate(id='EvenCols',  frames=colEvenFrames,   onPage=evenFooter)])

Title=CreateStyle("Title", "Times-Roman", 36, 100, 100, TA_CENTER)
Subtitle=CreateStyle("Subtitle", "Times-Italic", 24, 0, 0, TA_CENTER)
H1=CreateStyle("H1", "Times-Roman", 16, 0, 10, TA_CENTER)
H2=CreateStyle("H2", "Times-Roman", 14, 10, 7, TA_LEFT)
H3=CreateStyle("H3", "Times-Roman", 12, 10, 5, TA_LEFT)
Normal=CreateStyle("Normal", "Times-Roman", 10, 0, 1, TA_JUSTIFY)
story.append(Paragraph(" ", Normal))
story.append(Paragraph("The Polari Bible", Title))
story.append(Paragraph("5th Edition", Subtitle))
story.append(NextPageTemplate(['EvenCols','OddCols']))
story.append(PageBreak())
pre = re.compile("<polari><from>.*?</from><to>(.*?)</to></polari>")
for f in files:
    print("Processing {}".format(f))
    try:
        o = open(f, "rb")
    except IOError:
        print("Can't open {} for reading.".format(f))
    else:
        b = pickle.load(o)
        story.append(Spacer(0, 20 * mm))
        story.append(Paragraph(b.title.upper(),H1)) 
        for c in b.chapters:
            story.append(Paragraph("Chapter {}".format(c.index), H2))
            doc.clean_hanging()
            for v in c.verses:
                story.append(Paragraph(v, Normal))

doc.build(story)

但是,我有两个(相关的)问题似乎无法解决。

  1. 我想让 H1 标题在页面中居中。所以本质上是动态地插入一个标题,它跨越两列,并让普通文本围绕它流动。
  2. 为此,我还需要“平衡” H1 之前的列

所以像:

Column 1 Normal      Column 2 Normal
text. Column 1       text. Column 2
normal text.         normal text.
Column 1 Normal      Column 2 Normal
text. Column 1       text. Column 2
normal text.         normal text.

HEADING 1 TEXT WHICH SPANS BOTH COLUMNS

Column 1 Normal      Column 2 Normal
text. Column 1       text. Column 2
normal text.         normal text.
Column 1 Normal      Column 2 Normal
text. Column 1       text. Column 2
normal text.         normal text.

有什么建议么?

4

0 回答 0