问题
Using openpyxl
I've been able to get all the code in place to read .xlsx tables to generate .xml needed for a specific technical document tool. I'm trying to make the code general to work for just about any table someone on my team may generate and one issue that has bitten me is that some columns in the XML have formatting applied to save some typing.
For example, I have a table with a column for the line ID and the text, in Excel, reads as Test-001, Test-002, Test-003, etc. To save typing, I've set up a custom number format in Excel as "Test-"000, allowing someone to type any of the column's cells and have it convert to the Test-. The value returned when reading the cell value with openpyxl
returning .
Short of checking the number_format on every column and using a regular expression to extract the string and add it to the read value, is there a trivial way with openpyxl
(2.2.6) to have the the value return what is seen by the Excel user?
That would greatly generalize my code, as some tables might not have the ID column.
Here is a minimum working example of the python:
import openpyxl
import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
if __name__ == "__main__":
logging.info('Reading from ExampleWrkBook.xlsx')
wb = openpyxl.load_workbook('ExampleWrkBook.xlsx')
wrkSheet = wb.get_sheet_by_name('Sheet1')
logging.info('The first ID value in the sheet is "TestID-001", but openpyxl returns {0:s}'.format(str(wrkSheet['B3'].value)))
And an example spreadsheet can be found on DropBox here.
来源:https://stackoverflow.com/questions/32675739/reading-cell-value-with-applied-number-format