【问题标题】:openpyxl write to single column append()?openpyxl 写入单列追加()?
【发布时间】:2021-07-21 03:24:11
【问题描述】:

我正在尝试在符合特定条件的电子表格中查找数据。该电子表格大约有 9000 行和 16 列。我在第 1 列的任何行中匹配一个 ID,并从位于第 12 列的该行返回值。到目前为止,我正在找到我想要的数据并将其添加到列表中。我现在想将该信息逐行写入新工作表和单列。我正在使用 append() 但这是跨列写出来的。

更新:所以在继续审查这个。包含我想要的信息的单元格包含相当多的文本,它是一个带有新换行符的软件列表,如下所示(全部在一个单元格中):

远程主机上安装了以下软件: 7-Zip 19.00 (x64) [版本 19.00] Dell EqualLogic SAN 总部 [版本 3.4.0.9366] [安装于 2018 年 12 月 28 日] Google Chrome [版本 91.0.4472.77] [安装于 2021/06/01] Matrox 图形软件(仅删除) McAfee Agent [版本 5.7.2.162] Microsoft Visual J# 2.0 可再发行包 - SE (x64) 记事本++(64 位 x64)[版本 7.5.8] OpenSSL 1.1.0i (32-bit) [安装于 2018/08/30]

我正在尝试将其拆分为换行符并创建一个新工作表,其中每个软件项和版本都在一个列中。

在控制台中会返回:

[['Plugin Output: ', '远程主机上安装了以下软件:', '', 'McAfee Agent [版本 5.7.2.162]', 'Axway Desktop Validator [版本 4.12.0.4.0] [安装于 2021 年 4 月 26 日]'、'Microsoft Visual C++ 2013 x86 最小运行时 - 12.0.21005 [版本 12.0.21005] [安装于 2020 年 1 月 2 日]'、'McAfee Data Exchange Layer for MA [版本 6.0 .30278.0] [安装于 2021/04/03]'、'Microsoft Visual C++ 2010 x64 Redistributable - 10.0.40219 [版本 10.0.40219] [安装于 2020/10/01]'、'Microsoft Visual C++ 2010 x64 的修补程序Redistributable (KB2151757) [version 1]', 'Hotfix for Microsoft Visual C++ 2010 x64 Redistributable (KB2467173) [version 1]', 'Hotfix for Microsoft Visual C++ 2010 x64 Redistributable (KB2565063) [version 1]', 'Hotfix for Microsoft Visual C++ 2010 x64 Redistributable (KB982573) [版本 1]']]

我希望将数据返回附加到 A 列的新工作表中,但它会将其放入跨多列的单行中。有什么想法吗?

from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
import re
#load the workbook
wb = load_workbook('excel/TEST_FILES.xlsx', keep_vba=False)
#variable for activer worksheet, the first one
ws = wb.active

software = []

for row in ws.iter_rows(min_row=1, max_col=1,):
     #look into if this is needed
     for cell in row:       
         if cell.value == 20811:
             swlist = cell.offset(0,12).value
             dalist = swlist.split('\n')
             software.append(dalist)


 nws = wb.create_sheet("SWLIST")

 #appends each item to the new sheet & saves
 for item in software:
     nws.append(item)
     wb.save('excel/TEST_FILES.xlsx')
 print(software)

我已经取得了一些进展,并将上面的“附加代码”更新为以下内容。我不能让它附加一个索引引用,但不确定如何循环遍历每个项目。

更新代码:

 #appends each item to the new sheet & saves
 for item in software:
      nws.append({'A':item[5]})  
      wb.save('excel/TEST_FILES.xlsx')

####更新: 仍在通过附加工作。我没有运气将它附加到列出的所有软件的单个列中。我正在考虑将其制成字典然后附加它?见下文,但它返回语法错误。

for item in software:
     software = dict(zip('A',item)
     nws.append.(software)

错误:文件“D:\Dev_Projects\ExcelRangeLoop.py”,第 25 行 nws.append.(软件) ^ SyntaxError: 无效语法

*更新后的代码适用于我现在需要的地方

from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter

#load the workbook
wb = load_workbook('excel/TEST_FILES.xlsx', keep_vba=False)

#variable for active worksheet, the first one
ws = wb.active

software = []
for row in ws.iter_rows(min_row=1, max_col=1,):
    #look into if this is needed
     for cell in row:
         if cell.value == 20811:
             swlist = cell.offset(0,12).value
             dalist = swlist.split('\n')
             software = dalist
#Removes the unecessary info contained within the cell values
 software.remove('Plugin Output: ')
 software.remove('')
 del software[35:]
#new worksheet
 nws = wb.create_sheet("SWLIST")
#appends each item to the new sheet & saves
 for item in range(len(software)):
     nws.append({'A':software[item]})
print(software)
wb.save('excel/TEST_FILES.xlsx')

【问题讨论】:

  • 那么您想要将这些数据更新到具有不同行但具有相同列的新工作表中?您可以使用辅助索引来实现。
  • 感谢您的回复。是的,仅 A 列中的每个数据(软件名称)并继续逐行列出它们。第 1 - 100 行或其他任何内容。
  • 不确定帮助索引。我实际上正在测试以下内容,但出现语法错误:for item in software: software = dict(zip('A',item) nws.append.(software)
  • 我刚刚回答了一个代码示例。

标签: python openpyxl


【解决方案1】:

尝试使用索引来填充行。没有测试出这段代码,但它应该可以工作。

from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
import re
#load the workbook
wb = load_workbook('excel/TEST_FILES.xlsx', keep_vba=False)
#variable for activer worksheet, the first one
ws = wb.active

software = []

for row in ws.iter_rows(min_row=1, max_col=1,):
     #look into if this is needed
     for cell in row:       
         if cell.value == 20811:
             swlist = cell.offset(0,12).value
             dalist = swlist.split('\n')
             software.append(dalist)


nws = wb.create_sheet("SWLIST")

ind = nws.max_row
for i in range(len(software)):
    nws[f'A{i+ind}'] = software[i]

wb.save('excel/TEST_FILES.xlsx')
#appends each item to the new sheet & saves
print(software)

【讨论】:

  • 感谢您提供的信息。它让我走上了一条道路,我已经弄清楚了我的问题并进入了我的代码的下一部分。现在它 1. 搜索 ID 并找到它。 2. 创建新工作表并将值逐行解析为具有我需要的软件名称的单列。查看带有新代码的更新。
  • 我通常用openpyxl处理搜索xlsx文件的方式是有两个列表。第一个列表包含所有 ID,通常是一列,另一个列表是它们的索引,例如 ['item1,'item2'], [0,1]。然后,如果我正在寻找 item2,我将在第一个索引 1 中搜索它的索引,并与第二个列表进行映射。这样你就可以拥有它,你也可以使用 dict 来做同样的事情。想想这个,你也可以解决第2部分,记录软件名称及其索引,然后将两个索引同时排序,然后将值移动到新表中。
猜你喜欢
  • 2015-09-13
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 2017-09-06
  • 2015-09-08
  • 2011-12-24
相关资源
最近更新 更多