Flattening PDFs Using PyMuPDF, Unable to Pytest Code /u/Danny2200 Python Education

Okay so, I am making a pdf merging/flattening software that allows you to merge/flatten all of the folders in a file. It also has the functionality of flattening/merging all of the specified PDFs in a folder specified in the command line and captured using sys.argv. I have pytested the first functionality of the program which is merging PDFs by making a temp folder, some temp pdfs and then checking if the temp pdfs merge. However, when I try to test flattening the PDFs in the same way my command line basically crashes and I’m given many errors/warnings. Can anyone help? The first code exerpt is the test_merge_pdf.py program and the second one is just the flattening section of the flatten_pdf() function.

from project import merge_pdf import os import shutil from reportlab.pdfgen import canvas import tempfile def main(): test_merge_pdf() def test_merge_pdf(): with tempfile.NamedTemporaryFile(suffix=".pdf", delete=True) as temp_file: c = canvas.Canvas(temp_file.name) c.drawString(100, 750, 'Hello, World!') c.save() with tempfile.TemporaryDirectory() as temp_dir: shutil.copy(temp_file.name, temp_dir) merge_pdf((0, temp_dir)) dir_list = os.listdir(temp_dir) assert dir_list[0].startswith('merged') or dir_list[1].startswith('merged') if __name__ == '__main__': main() flat_pdfs = [] for pdf in pdfs: if n[1] != None: doc1 = pymupdf.open(f'{n[1]}/{pdf}') else: doc1 = pymupdf.open(pdf) out = pymupdf.open() mat = pymupdf.Matrix(2,2) for i, spage in enumerate(doc1): rect = spage.rect pix = spage.get_pixmap(matrix=mat) png = pix.tobytes('ppm') out._newPage(width=rect.width, height=rect.height) out_page = out.load_page(i) out_page.insert_image(rect, stream=png) if n[0] == 2: if n[1] != None: out.save(f'{n[1]}/flattened_{pdf}') else: out.save(f'flattened_{pdf}') 

submitted by /u/Danny2200
[link] [comments]

​r/learnpython Okay so, I am making a pdf merging/flattening software that allows you to merge/flatten all of the folders in a file. It also has the functionality of flattening/merging all of the specified PDFs in a folder specified in the command line and captured using sys.argv. I have pytested the first functionality of the program which is merging PDFs by making a temp folder, some temp pdfs and then checking if the temp pdfs merge. However, when I try to test flattening the PDFs in the same way my command line basically crashes and I’m given many errors/warnings. Can anyone help? The first code exerpt is the test_merge_pdf.py program and the second one is just the flattening section of the flatten_pdf() function. from project import merge_pdf import os import shutil from reportlab.pdfgen import canvas import tempfile def main(): test_merge_pdf() def test_merge_pdf(): with tempfile.NamedTemporaryFile(suffix=”.pdf”, delete=True) as temp_file: c = canvas.Canvas(temp_file.name) c.drawString(100, 750, ‘Hello, World!’) c.save() with tempfile.TemporaryDirectory() as temp_dir: shutil.copy(temp_file.name, temp_dir) merge_pdf((0, temp_dir)) dir_list = os.listdir(temp_dir) assert dir_list[0].startswith(‘merged’) or dir_list[1].startswith(‘merged’) if __name__ == ‘__main__’: main() flat_pdfs = [] for pdf in pdfs: if n[1] != None: doc1 = pymupdf.open(f'{n[1]}/{pdf}’) else: doc1 = pymupdf.open(pdf) out = pymupdf.open() mat = pymupdf.Matrix(2,2) for i, spage in enumerate(doc1): rect = spage.rect pix = spage.get_pixmap(matrix=mat) png = pix.tobytes(‘ppm’) out._newPage(width=rect.width, height=rect.height) out_page = out.load_page(i) out_page.insert_image(rect, stream=png) if n[0] == 2: if n[1] != None: out.save(f'{n[1]}/flattened_{pdf}’) else: out.save(f’flattened_{pdf}’) submitted by /u/Danny2200 [link] [comments] 

Okay so, I am making a pdf merging/flattening software that allows you to merge/flatten all of the folders in a file. It also has the functionality of flattening/merging all of the specified PDFs in a folder specified in the command line and captured using sys.argv. I have pytested the first functionality of the program which is merging PDFs by making a temp folder, some temp pdfs and then checking if the temp pdfs merge. However, when I try to test flattening the PDFs in the same way my command line basically crashes and I’m given many errors/warnings. Can anyone help? The first code exerpt is the test_merge_pdf.py program and the second one is just the flattening section of the flatten_pdf() function.

from project import merge_pdf import os import shutil from reportlab.pdfgen import canvas import tempfile def main(): test_merge_pdf() def test_merge_pdf(): with tempfile.NamedTemporaryFile(suffix=".pdf", delete=True) as temp_file: c = canvas.Canvas(temp_file.name) c.drawString(100, 750, 'Hello, World!') c.save() with tempfile.TemporaryDirectory() as temp_dir: shutil.copy(temp_file.name, temp_dir) merge_pdf((0, temp_dir)) dir_list = os.listdir(temp_dir) assert dir_list[0].startswith('merged') or dir_list[1].startswith('merged') if __name__ == '__main__': main() flat_pdfs = [] for pdf in pdfs: if n[1] != None: doc1 = pymupdf.open(f'{n[1]}/{pdf}') else: doc1 = pymupdf.open(pdf) out = pymupdf.open() mat = pymupdf.Matrix(2,2) for i, spage in enumerate(doc1): rect = spage.rect pix = spage.get_pixmap(matrix=mat) png = pix.tobytes('ppm') out._newPage(width=rect.width, height=rect.height) out_page = out.load_page(i) out_page.insert_image(rect, stream=png) if n[0] == 2: if n[1] != None: out.save(f'{n[1]}/flattened_{pdf}') else: out.save(f'flattened_{pdf}') 

submitted by /u/Danny2200
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *