Skip to content
Advertisement

A series of graphviz diagrams in a single pdf file

I create a graphviz diagram. Using below code.

  network1 = G.Digraph(
        graph_attr={...},
        node_attr={...},
        edge_attr={..} )

I add nodes

network1.node("node_edge_name",...)

… and edges

network1.edge("A", "B")

… and then call the below code. It creates me a pdf file and a dot file.

network1.view(file_name).

This way my diagram becomes very complicated. What I want is, to create a series of network objects instead of one and to visualize them in a single pdf file page by page. In the end, I hope to have multiple dot files and a single pdf file. Can someone describe if is there a way to do that and how?

Many thanks, Ferda

Advertisement

Answer

I used PdfFileReader, PdfFileWriter libraries to mege multiple pdf files into one. This was what I wanted.

pdf1File = open(file_name1, 'rb')
pdf2File = open(file_name2, 'rb')
pdf3File = open(file_name3, 'rb')
pdf4File = open(file_name4, 'rb')

# Read the files that you have opened
pdf1Reader = PyPDF2.PdfFileReader(pdf1File)
pdf2Reader = PyPDF2.PdfFileReader(pdf2File)
pdf3Reader = PyPDF2.PdfFileReader(pdf3File)
pdf4Reader = PyPDF2.PdfFileReader(pdf4File)

# Create a new PdfFileWriter object which represents a blank PDF document
pdfWriter = PyPDF2.PdfFileWriter()

# Loop through all the pagenumbers for the first document
for pageNum in range(pdf1Reader.numPages):
    pageObj = pdf1Reader.getPage(pageNum)
    pdfWriter.addPage(pageObj)

# Loop through all the pagenumbers for the second document
for pageNum in range(pdf2Reader.numPages):
    pageObj = pdf2Reader.getPage(pageNum)
    pdfWriter.addPage(pageObj)

# Loop through all the pagenumbers for the third document
for pageNum in range(pdf3Reader.numPages):
    pageObj = pdf3Reader.getPage(pageNum)
    pdfWriter.addPage(pageObj)

# Loop through all the pagenumbers for the fourth document
for pageNum in range(pdf4Reader.numPages):
    pageObj = pdf4Reader.getPage(pageNum)
    pdfWriter.addPage(pageObj)

# Now that you have copied all the pages in both the documents, write them into the a new document
pdfOutputFile = open(file_name5+"_Merged_Files.pdf", 'wb')
pdfWriter.write(pdfOutputFile)
Advertisement