覆盖 Python 中的现有 excel 文件时图表丢失
Graphs lost while overwriting to existing excel file in Python
我正在使用 openpyxl 写入现有文件,一切正常。但是,将数据保存到文件后,图形就会消失。
我了解 Openpyxl 目前仅支持在工作表中创建图表。现有工作簿中的图表将丢失。
Python 中是否有任何替代库可以实现此目的。我只想提供一些值,所以所有的图表和计算都发生在 excel.
中
谢谢。
目前(2.2 版)不可能。
我从 python 得到了一些替代解决方案来执行 excel 宏,这可能是上述问题的解决方案。
创建一个 ExcelWorkbook.xlsm 并编写 excel 宏(这很容易,excel 宏重新编码可能会对您有所帮助)您想要执行的任何任务并执行宏来自 python。图形和形状对象将是安全的。
openpyxl 可用于读写 ExcelWorkbook.xlsm
from openpyxl import Workbook, load_workbook
import os
import win32com.client
#The excel macro should be written and saved (Excelworkbook.xlsm) before using this code.
#Use macro codeExcelworkbook.xlsm to edit ExcelWorkBookContainGraph.xlsx
##################### Openpyxl #####################
#Open the Excelworkbook.xlsm (Macro workbook)
wb = load_workbook(filename='Excelworkbook.xlsm', read_only=False, keep_vba=True)
ws = wb.worksheets[0] #Worksheet will be sheet1[0]
##### Do the required task (read, write, copy..etc) in excel using openpyxl #####
#save Excelworkbook.xlsm
wb.save('Excelworkbook.xlsm')
#################### Run the excel macro #####################
if os.path.exists("Excelworkbook.xlsm"):
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=os.path.dirname(os.path.abspath(__file__))+"\Excelworkbook.xlsm")#, ReadOnly=1)
xl.Application.Run("Excelworkbook.xlsm!Module1.Macro1")
xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function.
xl.Application.Quit() # Comment this out if your excel script closes
del xl
############### Working with excel contains Graph ###############
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=os.path.dirname(os.path.abspath(__file__))+"\ExcelWorkBookContainGraph.xlsx")#, ReadOnly=1)
try:
xl.ActiveWorkbook.SaveAs("C:\ExcelExample\ExcelWorkBookContainGraph.xlsx")#Change the save path as per your requirment
xl.Application.Quit() # Comment this out if your excel script closes
del xl
except:
#If you get some error while saving kill the running excel task in background
print 'Error in saving file'
xl.Application.Quit() # Comment this out if your excel script closes
del xl
我正在使用 openpyxl 写入现有文件,一切正常。但是,将数据保存到文件后,图形就会消失。
我了解 Openpyxl 目前仅支持在工作表中创建图表。现有工作簿中的图表将丢失。
Python 中是否有任何替代库可以实现此目的。我只想提供一些值,所以所有的图表和计算都发生在 excel.
中谢谢。
目前(2.2 版)不可能。
我从 python 得到了一些替代解决方案来执行 excel 宏,这可能是上述问题的解决方案。
创建一个 ExcelWorkbook.xlsm 并编写 excel 宏(这很容易,excel 宏重新编码可能会对您有所帮助)您想要执行的任何任务并执行宏来自 python。图形和形状对象将是安全的。
openpyxl 可用于读写 ExcelWorkbook.xlsm
from openpyxl import Workbook, load_workbook
import os
import win32com.client
#The excel macro should be written and saved (Excelworkbook.xlsm) before using this code.
#Use macro codeExcelworkbook.xlsm to edit ExcelWorkBookContainGraph.xlsx
##################### Openpyxl #####################
#Open the Excelworkbook.xlsm (Macro workbook)
wb = load_workbook(filename='Excelworkbook.xlsm', read_only=False, keep_vba=True)
ws = wb.worksheets[0] #Worksheet will be sheet1[0]
##### Do the required task (read, write, copy..etc) in excel using openpyxl #####
#save Excelworkbook.xlsm
wb.save('Excelworkbook.xlsm')
#################### Run the excel macro #####################
if os.path.exists("Excelworkbook.xlsm"):
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=os.path.dirname(os.path.abspath(__file__))+"\Excelworkbook.xlsm")#, ReadOnly=1)
xl.Application.Run("Excelworkbook.xlsm!Module1.Macro1")
xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function.
xl.Application.Quit() # Comment this out if your excel script closes
del xl
############### Working with excel contains Graph ###############
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=os.path.dirname(os.path.abspath(__file__))+"\ExcelWorkBookContainGraph.xlsx")#, ReadOnly=1)
try:
xl.ActiveWorkbook.SaveAs("C:\ExcelExample\ExcelWorkBookContainGraph.xlsx")#Change the save path as per your requirment
xl.Application.Quit() # Comment this out if your excel script closes
del xl
except:
#If you get some error while saving kill the running excel task in background
print 'Error in saving file'
xl.Application.Quit() # Comment this out if your excel script closes
del xl