[Python] Problem of uploading multiple invoice to google drive

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    `The code does not work in uploading of multiple invoice of the same company. When a company has multiple invoice, but only one invoice is being uploaded for the company.

    # First pass: collect all files that can be uploaded
    print(f"Scanning directory: {INVOICE_DIR}")
    for filename in os.listdir(INVOICE_DIR):
    # print(filename) # Debugging print
    if not filename.endswith('.pdf'):
    print(f"Skipping file {filename} as it is not a PDF")
    continue

    # Use regex to capture the company name part after '_SG_'
    match = re.match(r'\d+_SG_(.+)\.pdf', filename, re.IGNORECASE) # Added IGNORECASE for robustness
    # print(match) # Debugging print
    if not match:
    print(f"Skipping file {filename} as it does not match the expected naming pattern")
    continue

    company_name_from_file = match.group(1)
    # print(f"Extracted company name: {company_name_from_file}") # Debugging print
    file_path = os.path.join(INVOICE_DIR, filename)

    # Pass the locally created normalized_seller_dict
    matching_folder = find_matching_folder(company_name_from_file, all_folders, normalized_seller_dict)
    # print(filename, matching_folder) # Debugging print

    if matching_folder:
    # Pass the MONTH constant
    month_folder_id = find_month_folder(drive_service, matching_folder['id'], MONTH)

    if month_folder_id:
    upload_batch.append({
    'file_path': file_path,
    'folder_id': month_folder_id,
    'folder_name': matching_folder['name']
    })
    else:
    # Use MONTH variable in the log message
    unmatched_invoices.append(f"{filename} - Required folder structure (Billing/{MONTH} Invoice or Invoices/{MONTH} Invoice) not found in {matching_folder['name']}")
    else:
    unmatched_invoices.append(f"{filename} - No matching company folder found for name '{company_name_from_file}'")

    print(f"Found {len(upload_batch)} files to upload.")
    print(f"Found {len(unmatched_invoices)} files that cannot be matched or uploaded.")

    # Second pass: batch upload files
    if upload_batch:
    print(f"\nStarting batch upload of {len(upload_batch)} files...")
    results = batch_upload_invoices(drive_service, upload_batch, creds)

    for result in results:
    if result['success']:
    matched_invoices.append(f"Successfully uploaded {result['filename']} to {result['folder_name']}")
    else:
    unmatched_invoices.append(f"{result['filename']} - Upload Error: {result['error']}")

    # Save unmatched invoices to log file
    if unmatched_invoices:
    with open(UNSUCCESS_LOG_FILE, 'w') as f:
    f.write('\n'.join(unmatched_invoices))
    print(f"\nUnmatched invoices have been logged to {UNSUCCESS_LOG_FILE}")

    # Save matched invoice to log file
    if matched_invoices:
    with open(SUCCESS_LOG_FILE, 'w') as f:
    f.write('\n'.join(matched_invoices))
    print(f"\nMatched invoices have been logged to {SUCCESS_LOG_FILE}")


    This is part of the code, I try to run the code. However, there is problem of uploading of the multiple invoice from same company.`

    Continue reading...

Compartilhe esta Página