Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中覆盖现有excel文件时图形丢失_Python_Excel_Openpyxl - Fatal编程技术网

在Python中覆盖现有excel文件时图形丢失

在Python中覆盖现有excel文件时图形丢失,python,excel,openpyxl,Python,Excel,Openpyxl,我正在使用openpyxl写入现有文件,一切正常。但是,数据保存到文件后,图形将消失 我知道Openpyxl目前只支持在工作表中创建图表。现有工作簿中的图表将丢失 Python中是否有其他库可以实现这一点。我只想输入一些值,所以所有的图形和计算都在excel中进行 谢谢。这目前(2.2版)不可能。这目前(2.2版)不可能。我从python中获得了一些替代解决方案来执行excel宏,这可能是上述问题的解决方案 创建一个ExcelWorkbook.xlsm并编写excel宏(这非常简单,excel宏

我正在使用openpyxl写入现有文件,一切正常。但是,数据保存到文件后,图形将消失

我知道Openpyxl目前只支持在工作表中创建图表。现有工作簿中的图表将丢失

Python中是否有其他库可以实现这一点。我只想输入一些值,所以所有的图形和计算都在excel中进行


谢谢。

这目前(2.2版)不可能。

这目前(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  

我从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