Python 3x win32com:从工作簿的工作表中复制使用的单元格
Python 3x win32com: Copying used cells from worksheets in workbook
我的工作簿中有 6 个作业 sheet。我想从 5 个工作 sheet 复制数据(除 header 之外的所有使用的单元格)并将它们粘贴到第一个。适用的代码片段:
`
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(mergedXL)
wsSIR = wb.Sheets(1)
sheetList = wb.Sheets
for ws in sheetList:
used = ws.UsedRange
if ws.Name != "1st sheet":
print ("Copying cells from "+ws.Name)
used.Copy()
`
used.Copy() 将复制所有使用过的单元格,但是我不想要任何工作sheet 中的第一行。我希望能够从每个 sheet 复制并将其粘贴到第一个 sheet 的第一个空白行中。因此,当第一个 sheet(不是我要复制到的 sheet 的单元格)粘贴到第一个 sheet 时,它们将从 A3 开始粘贴。每个后续粘贴都需要在第一个可用的空白行中进行。我可能没有很好地解释这一点,但希望得到一些帮助。还没有和 win32com 一起工作过。
我的一个旧脚本中也有这段代码,但我不明白它是如何复制内容的,以及这次我如何修改它以使其适合我:
ws.Range(ws.Cells(1,1),ws.Cells(ws.UsedRange.Rows.Count,ws.UsedRange.Columns.Count)).Copy()
wsNew.Paste(wsNew.Cells(wsNew.UsedRange.Rows.Count,1))
您是否考虑过使用 pandas?
import pandas as pd
# create list of panda dataframes for each sheet (data starts ar E6
dfs=[pd.read_excel("source.xlsx",sheet_name=n,skiprows=5,usecols="E:J") for n in range(0,4)]
# concatenate the dataframes
df=pd.concat(dfs)
# write the dataframe to another spreadsheet
writer = pd.ExcelWriter('merged.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()
如果我很了解你的问题,我认为这段代码可以解决问题:
import win32com.client
# create an instance of Excel
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
# Open the workbook
file_name = 'path_to_your\file.xlsx'
wb = excel.Workbooks.Open(file_name)
# Select the first sheet on which you want to write your data from the other sheets
ws_paste = wb.Sheets('Sheet1')
# Loop over all the sheets
for ws in wb.Sheets:
if ws.Name != 'Sheet1': # Not the first sheet
used_range = ws.UsedRange.SpecialCells(11) # 11 = xlCellTypeLastCell from VBA Range.SpecialCells Method
# With used_range.Row and used_range.Col you get the number of row and col in your range
# Copy the Range from the cell A2 to the last row/col
ws.Range("A2", ws.Cells(used_range.Row, used_range.Column)).Copy()
# Get the last row used in your first sheet
# NOTE: +1 to go to the next line to not overlapse
row_copy = ws_paste.UsedRange.SpecialCells(11).Row + 1
# Paste on the first sheet starting the first empty row and column A(1)
ws_paste.Paste(ws_paste.Cells(row_copy, 1))
# Save and close the workbook
wb.Save()
wb.Close()
# Quit excel instance
excel.Quit()
我希望它也能帮助您理解您的旧代码。
我的工作簿中有 6 个作业 sheet。我想从 5 个工作 sheet 复制数据(除 header 之外的所有使用的单元格)并将它们粘贴到第一个。适用的代码片段:
`
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(mergedXL)
wsSIR = wb.Sheets(1)
sheetList = wb.Sheets
for ws in sheetList:
used = ws.UsedRange
if ws.Name != "1st sheet":
print ("Copying cells from "+ws.Name)
used.Copy()
` used.Copy() 将复制所有使用过的单元格,但是我不想要任何工作sheet 中的第一行。我希望能够从每个 sheet 复制并将其粘贴到第一个 sheet 的第一个空白行中。因此,当第一个 sheet(不是我要复制到的 sheet 的单元格)粘贴到第一个 sheet 时,它们将从 A3 开始粘贴。每个后续粘贴都需要在第一个可用的空白行中进行。我可能没有很好地解释这一点,但希望得到一些帮助。还没有和 win32com 一起工作过。
我的一个旧脚本中也有这段代码,但我不明白它是如何复制内容的,以及这次我如何修改它以使其适合我:
ws.Range(ws.Cells(1,1),ws.Cells(ws.UsedRange.Rows.Count,ws.UsedRange.Columns.Count)).Copy()
wsNew.Paste(wsNew.Cells(wsNew.UsedRange.Rows.Count,1))
您是否考虑过使用 pandas?
import pandas as pd
# create list of panda dataframes for each sheet (data starts ar E6
dfs=[pd.read_excel("source.xlsx",sheet_name=n,skiprows=5,usecols="E:J") for n in range(0,4)]
# concatenate the dataframes
df=pd.concat(dfs)
# write the dataframe to another spreadsheet
writer = pd.ExcelWriter('merged.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()
如果我很了解你的问题,我认为这段代码可以解决问题:
import win32com.client
# create an instance of Excel
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
# Open the workbook
file_name = 'path_to_your\file.xlsx'
wb = excel.Workbooks.Open(file_name)
# Select the first sheet on which you want to write your data from the other sheets
ws_paste = wb.Sheets('Sheet1')
# Loop over all the sheets
for ws in wb.Sheets:
if ws.Name != 'Sheet1': # Not the first sheet
used_range = ws.UsedRange.SpecialCells(11) # 11 = xlCellTypeLastCell from VBA Range.SpecialCells Method
# With used_range.Row and used_range.Col you get the number of row and col in your range
# Copy the Range from the cell A2 to the last row/col
ws.Range("A2", ws.Cells(used_range.Row, used_range.Column)).Copy()
# Get the last row used in your first sheet
# NOTE: +1 to go to the next line to not overlapse
row_copy = ws_paste.UsedRange.SpecialCells(11).Row + 1
# Paste on the first sheet starting the first empty row and column A(1)
ws_paste.Paste(ws_paste.Cells(row_copy, 1))
# Save and close the workbook
wb.Save()
wb.Close()
# Quit excel instance
excel.Quit()
我希望它也能帮助您理解您的旧代码。