我正在尝试自动化办公室中的一项任务,其中我必须通过电子邮件手动向每个客户发送客户报表。我虽然可以使用 python 和 excel vba 自动执行此任务。 因此,在 Python 中,我正在获取一个包含所有客户端详细信息的文件,并为每个客户端在不同的文件中创建一个单独的选项卡,其中包含这些详细信息,其中宏以 xlsm 文件格式保存。之后,更新单元格中的客户编号并运行 Marco 以根据客户编号获取这些详细信息。在此阶段,在主选项卡中准备了一份声明。之后我尝试将主选项卡转换为 pdf 文件格式。下面是我的整个Python代码。
import pandas as pd
from openpyxl import load_workbook
import win32com.client
class Logic():
old_unique_ids = {} # stores clientid of old and new vales as dict
new_unique_ids = [] # query through excel and gets unique client ids
def __init__(self,input_file,output_file,pdf_path):
self.input_file = input_file
self.output_file = output_file
self.pdf_path = pdf_path
self.read_file = []
def replace_forward_slash_with_hyphen(self):
'''Replaces forward slash with hyphen and the before change client ids are saved in "old_unique_ids" '''
workbook = load_workbook(self.input_file)
for sheet_name in workbook.sheetnames:
sheet = workbook[sheet_name]
for row in sheet.iter_rows():
for cell in row:
if isinstance(cell.value, str) and "/" in cell.value: # checks if any cell value has /
old_value = cell.value
cell.value = cell.value.replace("/", "-")
Logic.old_unique_ids[cell.value] = old_value # append old and new id to dict
workbook.save(self.input_file)
def get_unique_values(self):
'''Gets unique value of new formated id and saves to new_unique_ids'''
self.read_file = pd.read_excel(self.input_file)
self.read_file['Date'] = self.read_file['Date'].dt.strftime('%d/%m/%y') # changes the data format in DF
Logic.new_unique_ids = self.read_file['ClientID'].unique().astype(str) # gets unique values and stores in str
def get_all_items(self):
"""loop through each id and get the detils from file and write the data to new excel file"""
for id in Logic.new_unique_ids:
data = self.read_file[(self.read_file['ClientID'] == id)] # loop and query through each id and get id details
with pd.ExcelWriter(self.output_file,
engine='openpyxl',
mode='a',
engine_kwargs={"keep_vba": True},
if_sheet_exists='replace') as writer:
data.to_excel(writer, # Write data to excel in seperate tab
sheet_name = id,
index=False,
startrow=0,
header=True)
def convertToPdf(self):
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
excel.DisplayAlerts = False
workbook_path = self.output_file
workbook = excel.Workbooks.Open(workbook_path)
ws = workbook.Sheets('Table') # Replace with with acutal sheet name
for n in Logic.new_unique_ids:
if len(Logic.old_unique_ids) != 0:
for i, j in Logic.old_unique_ids.items():
if n == i:
ws.Range('D4').Value = j
break # Exit the loop once a match is found
else:
ws.Range('D4').Value = n
#excel.Run("proFirst") # Replace with actual macro name
workbook.Save()
workbook.Close(SaveChanges=False)
excel.Quit()
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
o.DisplayAlerts = False
workbook_path = self.output_file
workbook = o.Workbooks.Open(workbook_path)
workbook.Worksheets('Table').Select()
pdf_file_path = f"{self.pdf_path}/{n}.pdf"
workbook.ActiveSheet.ExportAsFixedFormat(0, pdf_file_path)
input_file = 'input.xlsx'
output_file = r"C:\Users\madha\OneDrive\Desktop\Testing\output.xlsm"
pdf_path = "C:/Users/Testing/files"
app = Logic(input_file = input_file,output_file = output_file,pdf_path = pdf_path)
app.replace_forward_slash_with_hyphen()
app.get_unique_values()
app.get_all_items()
app.convertToPdf()
一切工作正常,直到运行宏并在主选项卡中使用 Marco 创建语句,但是当我尝试将该语句转换为 PDF 时,我的代码总是出现如下错误
pywintypes.com_error: (-2147352567, '发生异常。', (0, 'Microsoft Excel', '文档未保存。文档可能已打开,或者保存时遇到错误。', 'xlmain11.chm ', 0, -2146827284), 无)
我不确定我哪里出错了,我尝试了不同的方法来完成这项工作,但仍然不起作用。
有没有更简单的方法来完成这项工作?
如果 Excel 文档当前在 Excel 中打开,则可能不允许您的脚本访问 Excel 文档。运行之前尝试关闭所有 Excel 实例。