问题
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