1

I want to merge different files based on the initial string name. I have written the following code for the same.

import os
from pathlib import Path
from pypdf2 import pdfmerger

# Get the list of all files and directories
path = r'C:\\Users\\Divyanshu Singh\\Python_Scripts\\Doctor Payout Test\\files'
dir_list = os.listdir(path)

m = set()
for x in os.listdir(path):
    m.add(x[:45])
    
#  the set 'm' contains the following file names 
#  {'0a109230-9674-493c-ab2b-9833e2718f31_07_2022_',
#  '0a575dc2-c908-491f-bfe9-adef5d3f1c23_07_2022_',
#  '0ba8cf56-b114-472c-be2f-dfeac0bc5041_07_2022_',
#  '0d374417-7917-4484-bd6e-751d451ed58e_07_2022_'}

#I want to add files with similar initial strings but different strings at the end
    
for i in m:
    pdfs = [f'{m}1_10.pdf',f'{m}11_20.pdf',f'{m}21_30.pdf']
    merger = PdfMerger()
    for pdf in pdfs:
        merger.append(pdf)

    merger.write("result.pdf")
    merger.close()

Although i have already installed the pypdf2 and imported the pdfmerger, I am still getting the following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [17], in <cell line: 3>()
      1 import os
      2 from pathlib import Path
----> 3 from pypdf2 import pdfmerger
      5 # Get the list of all files and directories
      6 path = r'C:\\Users\\Divyanshu Singh\\Python_Scripts\\Doctor Payout Test\\files'

ModuleNotFoundError: No module named 'pypdf2'

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • First, please run `pip list` to check whether `PyPDF2` is installed. If it is on the list, fix the import `from PyPDF2 import PdfMerger`. It has to be exactly the same, case-sensitive. – dave.tdv Sep 04 '22 at 12:13
  • Does this answer your question? ["no module named PyPDF2" error](https://stackoverflow.com/questions/39241643/no-module-named-pypdf2-error) – Martin Thoma Dec 21 '22 at 23:38

1 Answers1

1

You should try changing the import statement from import pypdf2 to import PyPDF2

S_P_Y
  • 11
  • 2