Python script erroneously erases created chart in .xlsx file

Question:

I tried to write a script using Python which takes some specific values from all the .csv files stored in a hierarchy of folders. These values are copied at some specific cells in a destination file (.xlsx) which has already been created. The destination file also has some existing empty charts (in separate sheets) which are to be populated with the values provided by the script.

Unfortunately, after I run the script, although it works and I have the desired values copied in the cells, for some reason, the charts disappear. I haven’t managed to understand why, giving the fact that I didn’t work with anything that implied manipulating charts in my script.

Seeing that I couldn’t find any solution to this problem, I came to the conclusion that I should implement the plotting of the charts in my script, using the values I have. However, I would like to know if you have any idea why this happens.

Below is my code. I have to mention that I am new to Python.
Any suggestions about the problem or about a better writing of the code would be greatly appreciated.

# -*- coding: utf-8 -*-

import os
import glob
import csv
import openpyxl
from openpyxl import load_workbook

#getting the paths
def get_filepaths(directory):

    file_paths = []  # array that will contain the path for each file

    # going through the folder hierarchy
    for root, directories, files in os.walk(directory):
        for filename in files:
            if filename.endswith('.csv'): 
                filepath = os.path.join(root, filename) #concatenation
                file_paths.append(filepath)  # filling the array
                print filepath
                #print file_paths[0]

    return file_paths  


#extraction of the value of interest from every .csv file   
def read_cell(string, paths):
    with open(paths, 'r') as f:
        reader = csv.reader(f)        
        for n in reader:            
            if string in n:             
                cell_value = n[1]
                return cell_value



#array containing the path extracted for each file    
paths_F = get_filepaths('C:Dirmystufffiles')      

#destination folder
dest = r'C:Dirmystuffdestination.xlsx'   

#the value of interest 
target = "something"        

#array that will contain the value for each cell    
F30 = [];     

#obtaining the values    
for selection_F in paths_F:         
        if '30cm' in selection_CD:
            val_F30 = read_cell(target, selection_F);      F30.append(val_F30)
            #print val_F30


wb = load_workbook(dest)

#the sheet in which I want to write the values extracted from the files
ws3EV = wb.get_sheet_by_name("3EV")

   cell_E6 = ws10EV.cell( 'E6' ).value = int(F30[0])
cell_E7 = ws10EV.cell( 'E7' ).value = int(F30[1])
Asked By: Adrian B.

||

Answers:

I have been able to recreate your problem. After simply loading and saving an existing xlsx file using openpyxl, my chart was lost without making any changes.

Unfortunately, according to the documentation for openpyxl:

Warning

Openpyxl currently supports chart creation within a worksheet only.
Charts in existing workbooks will be lost.

It does though support the creation of a limited number of charts:

  • Bar Chart
  • Line Chart
  • Scatter Chart
  • Pie Chart

You could possibly recreate the required chart using these.

Answered By: Martin Evans
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.