0

I'm trying to write a python script to go through a bunch of XLSX files and make simple, consistent changes to a few cells. I tried using openpyxl which was able to make the changes but deleted the graphs and formatting of the workbook. I've read that win32com.client might be a better library for this but I can't get it on mac. Here's the code I used.

import openpyxl, os, re
from openpyxl import load_workbook
dir = '/Users/work/Desktop/energy_stuff/Rev3-Integrated Reports copy/'
for fil in os.listdir(dir):
    if not fil == 'Irvine ES Potential_rev 3_06102014':
    if re.search('Combined-15'):
        wb = load_workbook(fil)
        PD = wb.get_sheet_by_name("PD Inputs-Outputs")
        P90 = wb.get_sheet_by_name("P90 Summary")
        c = PD.cell('B15')
        c.value = '6:00'
        d = PD.cell('B16')
        d.value = '24:00'
        e = P90.cell('B12')
        e.value = '9:00'
        f = P90.cell('B13')
        f.value = '18:00'
        wb.save(fil)
    if re.search('Combined-60'):
        wb = load_workbook(fil)
        PD = wb.get_sheet_by_name("PD Inputs-Outputs")
        P90 = wb.get_sheet_by_name("P90 Summary")
        c = PD.cell('B15')
        c.value = '6:00'
        d = PD.cell('B16')
        d.value = '24:00'
        e = P90.cell('B12')
        e.value = 9
        f = P90.cell('B13')
        f.value = 18
        wb.save(fil)
    else:
        print 'Error: Neither Combined-15 nor Combined-60, check file name for typos'

Can anyone recommend a more appropriate library?

G Warner
  • 1,309
  • 1
  • 15
  • 27
  • According to [this](http://stackoverflow.com/questions/9233461/pywin32-com-objects-on-mac) COMs won't work, and I know `xlrd` and `xlwt``` only work on .xls files, so you may be out of luck. – wnnmaw Jun 12 '14 at 17:26

1 Answers1

0

If you're willing to write a bit of code, and ok to use coffeescript, I maintain a docx templating system called docxtemplater.

It doesn't modify the document except for certain tags.

They is not much docx-specific code, but rather for all of the xlsx, pptx documents. However, I don't yet support anything else than docx. Basically they is only one specific file that handles the docx specific: https://github.com/edi9999/docxtemplater/blob/master/coffee/docxTemplater.coffee , you might try to edit this to detect the xlsx tags.

Because you use python:

docxtemplater exports a command line, you could use that to call it from python

edi9999
  • 19,701
  • 13
  • 88
  • 127