写入新的 excel 文件,但不包含以前工作簿中的数据
Writing new excel file without data from previous workbook
我正在尝试编写一个程序来将固定矩阵中的字符串与 excel 文件中的 2 个特定列进行比较。到目前为止,我首先尝试实现与行中匹配项的比较。至此,矩阵中的一个字符串比较成功
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, 8)
if idm_matrix[0][0] in cell.value:
sheet.cell(row=2, column=1).value = cell.value
wb.save('Auswertung.xlsx')
问题:如何在没有上面加载的工作簿的情况下将匹配值保存在新文件中?
如果我在矩阵比较方面遇到更多困难,我会尽快回复您有关此项目的进一步帮助。
感谢您的帮助。
此致,亚历克斯
您需要创建一个新的工作簿来保存您的答案(比较结果)。像下面这样的东西。希望这对您有所帮助。
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
result_wb = xl.Workbook() #workbook to save your result.
result_sheet = result_wb.active #get the active sheet to save your result.
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_col+1):
cell = sheet.cell(row, col)
row_list.append(cell)
#adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[i][j] in row_list:
result_sheet.append(row_list)
result_wb.save('Auswertung.xlsx') #save the result workbook
@henjiFire:这就是代码现在的样子:
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_column + 1):
cell = sheet.cell(row, col)
row_list.append(cell.value)
# adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[0][0] in row_list:
if row_list[14] is not None and idm_matrix[0][1] in row_list[14]:
result_sheet.append(row_list)
我正在尝试编写一个程序来将固定矩阵中的字符串与 excel 文件中的 2 个特定列进行比较。到目前为止,我首先尝试实现与行中匹配项的比较。至此,矩阵中的一个字符串比较成功
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, 8)
if idm_matrix[0][0] in cell.value:
sheet.cell(row=2, column=1).value = cell.value
wb.save('Auswertung.xlsx')
问题:如何在没有上面加载的工作簿的情况下将匹配值保存在新文件中?
如果我在矩阵比较方面遇到更多困难,我会尽快回复您有关此项目的进一步帮助。
感谢您的帮助。 此致,亚历克斯
您需要创建一个新的工作簿来保存您的答案(比较结果)。像下面这样的东西。希望这对您有所帮助。
import openpyxl as xl
from IDM import idm_matrix
wb = xl.load_workbook('Auswertung_C33.xlsx')
result_wb = xl.Workbook() #workbook to save your result.
result_sheet = result_wb.active #get the active sheet to save your result.
sheet = wb['TriCad_Format']
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_col+1):
cell = sheet.cell(row, col)
row_list.append(cell)
#adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[i][j] in row_list:
result_sheet.append(row_list)
result_wb.save('Auswertung.xlsx') #save the result workbook
@henjiFire:这就是代码现在的样子:
for row in range(2, sheet.max_row + 1):
row_list = []
for col in range(1, sheet.max_column + 1):
cell = sheet.cell(row, col)
row_list.append(cell.value)
# adjust row,col offset to match your matrix index below, e.g. row-2, col-1. you might need another loop to loop through your matrix.
if idm_matrix[0][0] in row_list:
if row_list[14] is not None and idm_matrix[0][1] in row_list[14]:
result_sheet.append(row_list)