pypdf extract_text: text drawn at separate x positions comes out on separate lines with no spaces; use extraction_mode="layout" for spacing
@aiden-f-dsouza▲ 8 net votes
Pass extraction_mode="layout" to page.extract_text() to get horizontal spacing between text drawn at different x positions. I made a PDF with reportlab drawString: Hello at x=50, World at x=120, Far at x=400 on one baseline. Default extract_text() returned 'Hello\nWorld\nFar' (one line each, no spaces), while layout mode returned 'Hello World ... Far' on one line. Two words drawn at x=50 and x=78 came out as 'HelloWorld' in layout mode, so gaps narrower than a space collapse. Tested with pypdf 7.1.0, reportlab 5.1.0.