assert os.path.exists(filename), 'File %s could not be found' % filename # Open a spreadsheet workbook = openpyxl.Workbook() sheet = workbook.active # Loop through files for i in range(len(filenames)): # Open and read lines from text file f = open(filenames[i]) lines = f.readlines() f.close() # Determine column letter for a given filename col = g_c_l(i + 1) # Loop through lines and update corresponding cell value for j in range(len(lines)): cell = col + str(j + 1) sheet[cell] = lines[j] # Save and close the workbook workbook.save('combined.xlsx') workbook.close()
assert os.path.exists(filename), 'File does not exist' # Open filename w1 = openpyxl.load_workbook(filename) s1 = w1.active # Open new spreadsheet w2 = openpyxl.Workbook() s2 = w2.active # Size of contents in original file rows = s1.max_row cols = s1.max_column # Read in contents of spreadsheet for row in range(1, rows + 1): for col in range(1, cols + 1): orig = g_c_l(col) + str(row) dest = g_c_l(row) + str(col) s2[dest] = s1[orig].value # Write new spreadsheet object w2.save('_' + filename) # Close spreadsheet objects w1.close() w2.close()
# Verify filename provided assert os.path.exists(filename), 'File %s could not be found' % filename # Open a spreadsheet workbook = openpyxl.load_workbook(filename) sheet = workbook.active # Find dims of spreadsheet content rows = sheet.max_row cols = sheet.max_column # Loop through columns for col in range(1, cols + 1): cl = g_c_l(col) # Find column letter newfile = 'column_' + cl + '.txt' # Create new filename f = open(newfile,'w') # Create new file w/ filename # Loop through rows for row in range(1, rows + 1): cell = cl + str(row) line = sheet[cell].value if line == None: line = '' f.write(line) # Close file f.close() # Close the workbook
# Open workbook for editing w1 = openpyxl.load_workbook(filename) s1 = w1.active # Create new workbook w2 = openpyxl.Workbook() s2 = w2.active # Dimensions of original workbook rows = s1.max_row cols = s1.max_column # Create list of column letters cl = [] for col in range(1, cols + 1): cl.append(g_c_l(col)) for row in range(1, M): for col in range(cols): orig = dest = cl[col] + str(row) s2[dest] = s1[orig].value for row in range(M, rows + 1): for col in range(cols): orig = cl[col] + str(row) # Origin cell dest = cl[col] + str(row + N) # Destination cell s2[dest] = s1[orig].value
try: N = int(sys.argv[1]) except: print('Improper usage') exit() # Open spreadsheet object for editing workbook = openpyxl.Workbook() sheet = workbook.active # Loop through each cell to evaluate and set multiplication values for row in range(1, N + 1): for col in range(1, N + 1): cell = g_c_l(col + 1) + str( row + 1) # Determine string formatted cell name sheet[cell] = row * col # Set cell value to matrix value # Set format for column/row headers style = openpyxl.styles.Font(bold=True) # Insert row titles for row in range(1, N + 1): cell = 'A' + str(row + 1) sheet[cell] = row sheet[cell].font = style # Insert column titles for col in range(1, N + 1): cell = g_c_l(col + 1) + '1'
sheet = wb.get_sheet_by_name(sheetname) # Get max dimensions of sheet contents rows = sheet.max_row cols = sheet.max_column # Open new .csv file and create csv.writer object (basename, extension) = os.path.splitext(filename) new_filename = basename + '_' + sheetname + '.csv' path = os.path.join(dest, new_filename) f = open(path, 'w', newline = '') csv_file = csv.writer(f) # Loop through rows for row in range(1, rows + 1): # Create new line line = [] # Loop through columns for col in range(1, cols + 1): cell = g_c_l(col) + str(row) # Generate cell reference string line.append(sheet[cell].value) # Append cell values to the line # Write list object to .csv file csv_file.writerow(line) # Close the newly written .csv file f.close()