我撰寫了一個程式,它從一張紙中獲取資料并將其粘貼到另一張紙中。但是,當我嘗試保存作業簿時,它給了我一個錯誤。檔案名應與在前一行中輸入的檔案名相同,但我不確定如何使保存功能對作業簿起作用。我試過使用變數 input 和它的 str 但都沒有奏效。
unit_list = input("Please enter the file name: ")
unit_list = load_workbook(unit_list)
all_units = unit_list["Sheet1"]
picked_units = unit_list["Sheet2"]
row_count = all_units.max_row
col_count = all_units.max_column
need_units = int(input("How many units need to be checked? "))
nul = []
while len(nul) 1 <= need_units:
unit = rand_seed(row_count)
if unit not in nul:
nul.append(unit)
for j in range(1, col_count 1):
c = all_units.cell(row=unit 1, column=j)
picked_units.cell(row=len(nul), column=j).value = c.value
unit_list.save(filename=unit_list)
uj5u.com熱心網友回復:
您在代碼中使用變數名稱unit_list來表示兩種不同的含義。首先是檔案名,然后是您從該檔案名打開的整個作業簿。
您嘗試保存作業簿的行嘗試使用兩種含義:
unit_list = input("Please enter the file name: ") # first use
unit_list = load_workbook(unit_list) # second use
#...
unit_list.save(filename=unit_list) # this needs to refer to the both versions of unit_list
嘗試這樣的事情:
filename = input("Please enter the file name: ") # new name for this variable
unit_list = load_workbook(filename) # use it here
#...
unit_list.save(filename=filename) # now we have two different variables to use down here
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/353931.html
上一篇:在列中查找下一個非空單元格
下一篇:基于概率的分布
