我有很多excel文件,需要编译成一个excel文件,然后将编译后的excel文件复制到某个工作表中的现有excel文件(带有宏/ .xlsm)中。
我解决了第一个问题(将多个excel文件编译成一个excel文件)。得到的数据以.csv格式保存。得到的文件如下所示。
在此之前没有任何问题。下一步,我正在努力找出如何做到这一点。
从结果的dataframe,我想“复制和粘贴”数据到现有的excel文件与宏(.xlsm)在工作表“源”在相应的标题。现有的excel文件如下所示。
正如您可能从上面的图片中看到的,我想跳过在A列中写入任何数据,因为这一列中的单元格充满了公式。我希望将结果数据在B列中写入到现有excel文件中的Q列中。但是,在编写数据之前,我希望删除所有单元格中的所有现有数据(A列中的单元格除外)。
基本上,我想做以下几点:
任何反馈都将不胜感激!谢谢!
致以敬意,
阿诺德
发布于 2017-01-12 10:52:11
抱歉回来更新我的问题有点晚了。最后,我解决了openpyxl包的问题。
这是我的最后代码:
import openpyxl
import os
import string
import pandas as pd
import numpy as np
path = #folder directory
target_file = #excel filename
sheetname = #working sheet that you wish to work on with
filename = os.path.join(path, target_file)
wb = openpyxl.load_workbook(filename, keep_vba=True)
sheet = wb.get_sheet_by_name(sheetname)
# To Erase All Values within Selected Columns
d = dict()
for x, y in zip(range(1, 27), string.ascii_lowercase):
d[x] = y.upper()
max_row = sheet.max_row
max_col = sheet.max_column
for row in range(max_row):
row += 1
if row == 1: continue
for col in range(max_col):
col += 1
if col == 1: continue
sheet['{}{}'.format(d[col], row)] = None
# To Write Values to the Blank Worksheet
path_dataframe = # folder directory to the csv file
target_compiled = # csv filename
filename_compiled = os.path.join(path_compiled, target_compiled)
compiled = pd.read_csv(filename_compiled, low_memory=False, encoding = "ISO-8859-1")
for row in range(len(compiled.index)):
row += 1
if row == 1: continue # I do not want to change the value in row 1 in excel file because they are headers
for col in range(max_col):
col += 1
if col == 1: continue # I do not want to change the values in column 1 in excel file since they contain formula
value = compiled.iloc[row-2][col-2]
if type(value) is str: value = value
elif type(value) is np.float64: value = float(value)
elif type(value) is np.int64: value = int(value)
sheet['{}{}'.format(d[col], row)] = value
wb.save(filename)
发布于 2020-06-25 12:59:33
我找到了基于openpyxl的折叠液。我学到的是,xlsxwriter无法打开现有的excel文件。因此,我的方法是基于openpyxl的。
import pandas as pd
import openpyxl # one excel reader/writer compatible with pandas
book = openpyxl.load_workbook('input.xlsm', keep_vba = True) # Load existing .xlsm file
with pd.ExcelWriter('output.xlsm', engine='openpyxl') as writer: # open a writer instance with the filename of the
writer.book = book # Hand over input workbook
writer.sheets = dict((ws.title, ws) for ws in book.worksheets) # Hand over worksheets
writer.vba_archive = book.vba_archive # Hand over VBA information
df_write.to_excel(writer, sheet_name = 'Sheet1', columns = ['A'],
header = False, index = False,
startrow = 1, startcol = 0)
# Writes a column A of the Dataframe into the excel sheet 'Sheet1', which can
# already be present in input.xlsm, to row 1, col 0
writer.save()
发布于 2017-01-03 20:34:17
由于您的csv导入到电子表格可以使用Excel宏使用QueryTables处理,所以可以考虑让Python与一个COM接口连接到Excel。所有以前的宏代码都保持原样,因为除了单元数据之外,没有任何内容被覆盖。备注:下面假设您使用的是Excel。
使用win32com
库,Python几乎可以复制VBA所做的任何事情。实际上,您将了解到VBA是Office应用程序中的一个附加引用,从来不是一个本机内置对象,并且执行相同的COM接口!请参阅IDE中的Tools\References
中的第一个选定项。
import pandas as pd
import win32com.client as win32
# ...same pandas code...
macrofile = "C:\\Path\\To\\Macro\\Workbook.xlsm"
strfile = "C:\\Path\\To\\CSV\\Output.csv"
df.to_csv(strfile)
try:
xl = win32.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(macrofile)
# DELETE PREVIOUS DATA
wb.Sheets("Source").Range("B:Q").EntireColumn.Delete()
# ADD QUERYTABLE (SPECIFYING DESTINATION CELL START)
qt = wb.Sheets("Source").QueryTables.Add(Connection="TEXT;" + strfile,
Destination=wb.Sheets(1).Cells(2, 2))
qt.TextFileParseType = 1
qt.TextFileConsecutiveDelimiter = False
qt.TextFileTabDelimiter = False
qt.TextFileSemicolonDelimiter = False
qt.TextFileCommaDelimiter = True
qt.TextFileSpaceDelimiter = False
qt.Refresh(BackgroundQuery=False)
# REMOVE QUERYTABLE
for qt in wb.Sheets("Source").QueryTables:
qt.Delete()
# CLOSES WORKBOOK AND SAVES CHANGES
wb.Close(True)
except Exception as e:
print(e)
finally:
qt = None
wb = None
xl = None
或者,在VBA中创建一个新的宏(放置在独立模块中)并让Python调用它,将csv文件路径作为参数传递:
VBA
Public Sub ImportCSV(strfile As String)
Dim qt As QueryTable
ThisWorkbook.Sheets("Source").Range("B:Q").EntireColumn.Delete
' ADD QUERYTABLE
With ThisWorkbook.Sheets("Source").QueryTables.Add(Connection:="TEXT;" & strfile, _
Destination:=ThisWorkbook.Sheets(1).Cells(2, 2))
.TextFileParseType = xlDelimited
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.Refresh BackgroundQuery:=False
End With
' REMOVE QUERYTABLE
For Each qt In ThisWorkbook.Sheets(1).QueryTables
qt.Delete
Next qt
Set qt = Nothing
End Sub
Python
import pandas as pd
import win32com.client as win32
# ...same pandas code...
macrofile = "C:\\Path\\To\\Macro\\Workbook.xlsm"
strfile = "C:\\Path\\To\\CSV\\Output.csv"
df.to_csv(strfile)
try:
xl = win32.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(macrofile)
xl.Application.Run('ImportCSV', strfile)
wb.Close(True)
xl.Quit
except Exception as e:
print(e)
finally:
wb = None
xl = None
https://stackoverflow.com/questions/41439132
复制相似问题