Python:有没有办法在 1 个循环中写入 Excel 单元格并导出 PDF
Python: Is there way how to write in to Excel Cell and export PDF in 1 loop
我的项目确实遇到了棘手的情况。我正在尝试更新 Excel Sheet 并在一个循环中将其导出为 PDF。
目前我认为最好的是 openpyxl 库。
问题是写入和打印功能都以不同的方式打开 Excel.. 使用:
book = openpyxl.load_workbook(excel_file) 和
wb = excel.Workbooks.Open(excel_file).
这两个功能相互交叉并产生权限问题(至少它看起来像)加上崩溃的 Jupyter :)。
请问有什么优雅的方法可以做到这一点,或者我真的需要 2 个循环?
错误调用示例:
PermissionError:[Errno 13] 权限被拒绝:'C:/Users/admin/test_files/dir#$$.xlsx'
代码如下所示:
def update_directory():
excel_file = r'C:/Users/admin/test_files/doo.xlsx'
excel = client.DispatchEx("Excel.Application")
excel.Visible = 0
folder_selected = filedialog.askdirectory()
os.chdir(folder_selected)
for root, dirs, files in os.walk(".", topdown=False):
for name in dirs:
a_pth = os.getcwd()
pdf_file = os.path.join(a_pth,name," ")+"Dic_"+"%s.pdf" % name
book = openpyxl.load_workbook(excel_file)
sheet= book['Sheet1']
sheet.cell(row=4, column=6).value = name
book.save(excel_file)
wb = excel.Workbooks.Open(excel_file)
ws = wb.Worksheets[1]
ws.SaveAs(pdf_file, FileFormat=57)
wb.Close() # <- need to be part of loop (comment from Amiga500). File save
# prompt from Excell present.
excel.Exit()
有条目
wb.application.displayalerts = False
刚好在
之前插入
wb.Close()
行似乎对我有用,所以代码片段类似于
book = openpyxl.load_workbook(excel_file)
sheet= book['Sheet1']
sheet.cell(row=4, column=6).value = name
book.save(excel_file)
wb = excel.Workbooks.Open(excel_file)
ws = wb.Worksheets[1]
ws.SaveAs(pdf_file, FileFormat=57)
wb.application.displayalerts = False #This stops the popup asking for a save
wb.Close() # <- need to be part of loop (comment from Amiga500). File save
# prompt from Excell present.
注意 wb.Close() 与内部 for 循环的其余部分缩进相同。
我的项目确实遇到了棘手的情况。我正在尝试更新 Excel Sheet 并在一个循环中将其导出为 PDF。 目前我认为最好的是 openpyxl 库。 问题是写入和打印功能都以不同的方式打开 Excel.. 使用:
book = openpyxl.load_workbook(excel_file) 和 wb = excel.Workbooks.Open(excel_file).
这两个功能相互交叉并产生权限问题(至少它看起来像)加上崩溃的 Jupyter :)。
请问有什么优雅的方法可以做到这一点,或者我真的需要 2 个循环?
错误调用示例: PermissionError:[Errno 13] 权限被拒绝:'C:/Users/admin/test_files/dir#$$.xlsx'
代码如下所示:
def update_directory():
excel_file = r'C:/Users/admin/test_files/doo.xlsx'
excel = client.DispatchEx("Excel.Application")
excel.Visible = 0
folder_selected = filedialog.askdirectory()
os.chdir(folder_selected)
for root, dirs, files in os.walk(".", topdown=False):
for name in dirs:
a_pth = os.getcwd()
pdf_file = os.path.join(a_pth,name," ")+"Dic_"+"%s.pdf" % name
book = openpyxl.load_workbook(excel_file)
sheet= book['Sheet1']
sheet.cell(row=4, column=6).value = name
book.save(excel_file)
wb = excel.Workbooks.Open(excel_file)
ws = wb.Worksheets[1]
ws.SaveAs(pdf_file, FileFormat=57)
wb.Close() # <- need to be part of loop (comment from Amiga500). File save
# prompt from Excell present.
excel.Exit()
有条目
wb.application.displayalerts = False
刚好在
之前插入wb.Close()
行似乎对我有用,所以代码片段类似于
book = openpyxl.load_workbook(excel_file)
sheet= book['Sheet1']
sheet.cell(row=4, column=6).value = name
book.save(excel_file)
wb = excel.Workbooks.Open(excel_file)
ws = wb.Worksheets[1]
ws.SaveAs(pdf_file, FileFormat=57)
wb.application.displayalerts = False #This stops the popup asking for a save
wb.Close() # <- need to be part of loop (comment from Amiga500). File save
# prompt from Excell present.
注意 wb.Close() 与内部 for 循环的其余部分缩进相同。