15

I'm writing a python script to merge multiple PDFs into one to put into an output folder. Each PDF will be in a different folder with an employee name on it and it will need to be combined with the corresponding pdf in another folder. Not all employees will have each file so I included that in the logic.

The problem I am having currently is that PdfFileMerger expects a string when appending files and the pathlib library returns a windowpath object. That will not easily convert to a string. I'm getting this information about PyPDF2 from this post pypdf Merging multiple pdf files into one pdf. I'm new to the pathlib library is there a conversion I should be doing or should I be getting a different path object?

from PyPDF2 import PdfFileMerger, PdfFileReader
from pathlib import Path

tc = Path('totalcomp')
merger = PdfFileMerger()


for i in tc.iterdir():
pdfs = []
try:
    pdfs.append(Path(f'profitshare/{i.name}'))
    pdfs.append(Path(f'merit/{i.name}'))
finally:
    pdfs.append(i)
    for pdf in pdfs:
        output = i.name
        merger.append(pdf, 'rb')
    merger.write(Path(f'/output/{i.name}'))
Jarl2.0
  • 205
  • 1
  • 3
  • 8

1 Answers1

18

According to pathlib documentation

The string representation of a path is the raw filesystem path itself (in native form, e.g. with backslashes under Windows), which you can pass to any function taking a file path as a string:

>>> p = PurePath('/etc')
>>> str(p)
'/etc'
>>> p = PureWindowsPath('c:/Program Files')
>>> str(p)
'c:\\Program Files'

so the solution is to wrap Path() objects with str() function:

from PyPDF2 import PdfFileMerger, PdfFileReader
from pathlib import Path

tc = Path('totalcomp')
merger = PdfFileMerger()


for i in tc.iterdir():
pdfs = []
try:
    pdfs.append(str(Path(f'profitshare/{i.name}')))
    pdfs.append(str(Path(f'merit/{i.name}')))
finally:
    pdfs.append(i)
    for pdf in pdfs:
        output = i.name
        merger.append(pdf, 'rb')
    merger.write(str(Path(f'/output/{i.name}')))

BTW there is another one pathlib's method which returns a path as a string: as_posix()

Return a string representation of the path with forward slashes (/):

>>> p = PureWindowsPath('c:\\windows')
>>> str(p)
'c:\\windows'
>>> p.as_posix()
'c:/windows'
Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
and1er
  • 749
  • 8
  • 18