Update values of multiple Excel workbooks with openpyxl

独自空忆成欢 提交于 2019-12-11 19:49:24

问题


I have a lot of Excel worksheets with a lot of same values that I should update with new values periodically so I googled and found Automate the Boring Stuff. Chapter 12 – Working with Excel Spreadsheets.

My workbooks have a header from row 1 to 6 with a couple of images and some cells are merged. I followed the Chapter 12 and found a simple script that should do what I need, this is what I came up with

import openpyxl
from PIL import *

excelFile = openpyxl.load_workbook('V23 R.0.xlsx')

sheet = excelFile.get_sheet_by_name(u'Caratula')

thickness_update = {'12.7': 12.5,
                    '6.4': 6.35,
                    '7.9': 8.0,
                    '4.8': 4.75}

for rowNum in range(3, 30):
    nominalThickness = sheet.cell(row=rowNum, column=6).value
    if nominalThickness in thickness_update:
        sheet.cell(row=rowNum, column=6).value = thickness_update[nominalThickness]

excelFile.save('V23 R.1.xlsx')

I know the code in the Chapter 12 was intended to replace the values of column 2 based on the values of column 1 but I thought that my code should change the values of column 6 with the ones in thickness_update but it does nothing. The column 6 has the same four values in different orders.

To see if openpyxl was accessing to the .xlsx file correctly I did

sv = sheet.cell(row=1, column=2).value
print sv

and it returned None. That happened whit whatever the cell I was asking for.

As I said before, the header has two images and I get the following warning message

/home/gval/.local/lib/python2.7/site-packages/openpyxl/worksheet/header_footer.py:49: UserWarning: Cannot parse header or footer so it will be ignored
  warn("""Cannot parse header or footer so it will be ignored""")
/home/gval/.local/lib/python2.7/site-packages/openpyxl/reader/drawings.py:50: UserWarning: wmf image format is not supported so the image is being dropped
  warn(msg)

EDIT: This is what the first rows looks like

Couple of images in a two merged cells |  Something |  Something 
Something |  Something    | Something  |  Something |  Something
Something in a merged cells            |  Something |  Something

The data can be some numbers and some alphanumeric inputs.

来源:https://stackoverflow.com/questions/53733105/update-values-of-multiple-excel-workbooks-with-openpyxl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!