如何使用openpyxl将整个列(或我可以迭代的单元格)格式化为文本格式

时间:2019-07-09 21:11:26

标签: python openpyxl

在python脚本将excel列从一个文件复制到另一个文件之前,我需要如何将excel列更改为文本的帮助

当前正在使用openpyxl。 但向任何能够胜任工作且不会丢失工作表中其他格式或颜色的库开放

问题是,当其中包含日期时,我需要将其复制为纯文本而不是日期

所以当有人键入2019年6月17日... 而不是变成2019-06-17 0:00:00

认为正确的位置应该是在加载每个工作簿之后。 我知道是否可以手动更改文件,以便将所需的列设置为“文本”格式,脚本可以完美运行,并且所有内容均可按预期进行复制。

我正在openopenxl中寻找一个与右键单击excel中的单元格,选择格式并选择“文本”相同的功能

# open both files, load them in, 
#Change Columns F and H to Text Formatting before doing the next part of the script







#Begin Openpyxl Section

orig_wb = load_workbook('old_qc_report.xlsx')
new_wb = load_workbook('new_qc_report.xlsx')


sheetlist = orig_wb.get_sheet_names()
sheet = orig_wb.get_sheet_by_name(sheetlist[0])
sheetlist2 = new_wb.get_sheet_names()
sheet2 = new_wb.get_sheet_by_name(sheetlist[0])


s1rownumber = 1
for s1row in sheet.iter_rows(min_row=2, max_row= sheet.max_row, min_col=6, max_col=6, values_only=True):
  s1rownumber +=1
  for cell in s1row:
    if cell != None:
#      print(cell)
      transfer_cust_rec = cell
      product_description_ref = sheet[str("A" + str(s1rownumber))].value
      order_No_ref = sheet[str("C" + str(s1rownumber))].value
#      print(product_description_ref)
#      print(order_No_ref)

      s2rownumber = 1
      for s2row in sheet2.iter_rows(min_row = 2, min_col=1, max_col=1, values_only=True):
        s2rownumber +=1
        for cell2 in s2row:
          if cell2 == product_description_ref:
            if sheet2[str("C" + str(s2rownumber))].value == order_No_ref:
              sheet2[str("F" + str(s2rownumber))].value = transfer_cust_rec


s1rownumber = 1
for s1row in sheet.iter_rows(min_row=2, max_row= sheet.max_row, min_col=8, max_col=8, values_only=True):
  s1rownumber +=1
  for cell in s1row:
    if cell != None:
#      print(cell)
      transfer_notes = cell
      product_description_ref = sheet[str("A" + str(s1rownumber))].value
      order_No_ref = sheet[str("C" + str(s1rownumber))].value
#      print(product_description_ref)
#      print(order_No_ref)

      s2rownumber = 1
      for s2row in sheet2.iter_rows(min_row = 2, min_col=1, max_col=1, values_only=True):
        s2rownumber +=1
        for cell2 in s2row:
          if cell2 == product_description_ref:
            if sheet2[str("C" + str(s2rownumber))].value == order_No_ref:
              sheet2[str("H" + str(s2rownumber))].value = transfer_notes


new_wb.save(outfilepath)


wb.close

os.remove("old_qc_report.xlsx")
os.remove("new_qc_report.xlsx")

print("\nThis program took %2.3f seconds to run\n"%(time.time() - start_time))
time.sleep(3)

1 个答案:

答案 0 :(得分:0)

编辑 仅包含OpenPyXL


由于您要在保存Excel文件后保持 的格式-不仅仅是在OpenPyXL版本期间,所以将数字格式用作'@'

    import openpyxl

    wb = openpyxl.load_workbook('input.xlsx')
    ws = wb.active

    for row in ws.iter_rows():
        for cell in row:
            # only relevant column and without header
            if cell.column_letter == 'D' and cell.row > 1:
                ws[cell.coordinate].number_format = '@'

    wb.save('output.xlsx')

输出

enter image description here

希望它会有所帮助(: