import openpyxl
def read_excel():
# Create a new workbook and add a worksheet
workbook = openpyxl.Workbook()
worksheet = workbook.create_sheet('Sheet1')
# Use a while loop to generate and write the rows one at a time
row = 1
n = 1
while row <= 6306456: # 1048576*6+15000
# If the row number is greater than 1,048,576, create a new worksheet
# and reset the row number to 1
c = row
# if row > 1048576:
if c > (1048576 * n):
worksheet = workbook.create_sheet(f'Sheet{row // 1048576 + 1}')
c = c - (1048576 * n)
n = (row // 1048576) + 1
# Write the row number to the first column of the worksheet
worksheet.cell(row=c, column=1).value = row
row += 1
# Print the progress of the operation
print(f'{row} of 6306456 rows written')
# Save the workbook
workbook.save('row_file.xlsx')
if __name__ == '__main__':
# Call the read_excel function
read_excel()
Above is the script,but it doesn't work,and it showed row is over 1048576.
I have 1048576*6+15000=6306456 rows of data to write in a single .xlsx file.
Could someome please help to fix the script when using openpyxl to create another new worksheets when a sheet reached 1048576 rows.
Therefore,the result is a .xlsx file with 7 worksheets to store more than 6306456 rows.
Thanks a lot.