Get first empty row of sheet in Excel file with Python

Question:

I need to find the first empty row in an Excel file, i am currently using Openpyxl with Python.
I couldn’t find any method that does what i need so i am trying to make my own. This is my code:

 book = load_workbook("myfile.xlsx")
 ws = book.worksheets[0]
 for row in ws['C{}:C{}'.format(ws.min_row,ws.max_row)]:
    for cell in row:
        if cell.value is None:
            print cell.value
            break

I am iterating through all cells in the “C” column and i am “breaking” if the cell is empty. The problem is that it won’t break, it’ll just keep print out “None” values.

Thanks

Asked By: user9704406

||

Answers:

This way your loop will stop if it encounters any empty cell in a row.
If you want the row wo be completely empty you can use all.

book = load_workbook("myfile.xlsx")
ws = book.worksheets[0]
for cell in ws["C"]:
    if cell.value is None:
        print cell.row
        break
else:
    print cell.row + 1

Update to the question in the comments:

ws["C"] will get a slice from C1:CX where X is the last filled cell in any column. So if the C column happens to be the longest column and every entry is filled you will only get cells with cell is not None so you won’t break out of the loop. If you didn’t break out of the loop you will enter the else block and since you looped till the last filled row, the first empty row will be the next one.

Answered By: pask

There is a built-in worksheet property “max_row” in openpyxl:
https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.worksheet.html#openpyxl.worksheet.worksheet.Worksheet.max_row
max_row: an integer defining the maximum row index containing data

Answered By: Tim C
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.Scanner;

public class CsvToXlsxConverter {
    public static void main(String[] args) {
        // Specify the paths for the CSV and XLSX files
        String csvFilePath = "path_to_input.csv";
        String xlsxFilePath = "path_to_output.xlsx";

        try {
            FileInputStream csvInputStream = new FileInputStream(new File(csvFilePath));
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Sheet1");

            // Read and parse CSV file
            Scanner scanner = new Scanner(csvInputStream);
            int rowNum = 0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] columns = line.split(",");

                Row row = sheet.createRow(rowNum++);
                int cellNum = 0;
                for (String column : columns) {
                    Cell cell = row.createCell(cellNum++);
                    cell.setCellValue(column);
                }
            }

            // Save as XLSX
            FileOutputStream xlsxOutputStream = new FileOutputStream(xlsxFilePath);
            workbook.write(xlsxOutputStream);
            xlsxOutputStream.close();
            workbook.close();
            scanner.close();

            System.out.println("CSV file converted to XLSX successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Answered By: Thanush Raj