python:exelprocess
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:exelprocess [2020/01/19 15:20] – [Using sheet(xlrd.sheet.Sheet)] admin | python:exelprocess [2022/10/29 16:15] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Exel Processing ====== | ====== Exel Processing ====== | ||
+ | ===== Function in Exel ===== | ||
+ | Trước khi xử lý các hàm tìm kiếm Vlookup and Hlookup, If trong exel cần phải format dữ liệu và sort trước -> Lúc đó hàm xử lý mới chạy đúng | ||
+ | VLOOKUP($B2, | ||
===== Install ===== | ===== Install ===== | ||
* Install on windows:< | * Install on windows:< | ||
Line 11: | Line 14: | ||
pip install xlutils | pip install xlutils | ||
</ | </ | ||
- | ===== using xlrd ===== | + | ===== Using xlrd ===== |
Basic APIs: | Basic APIs: | ||
* **xlrd.open_workbook(' | * **xlrd.open_workbook(' | ||
Line 95: | Line 98: | ||
print sheet_info.dump() | print sheet_info.dump() | ||
</ | </ | ||
- | ===== Parse Shop.xls ==== | + | ==== Parse xls file ==== |
<code python> | <code python> | ||
import xlrd | import xlrd | ||
- | + | from xlrd.book import Book | |
- | def parseshopxls(filename): | + | from xlrd.sheet import Sheet |
- | | + | nhapkhaufile |
- | 'config_name':'',# | + | xuatkhaufile = 'xuatkhau.xlsx' |
- | | + | constantsfile = 'constants.xlsx' |
- | | + | def parsexls(filename): |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | } | + | |
- | shop_cat = [] | + | |
- | shop_products = [] | + | |
xlsfile = xlrd.open_workbook(filename) | xlsfile = xlrd.open_workbook(filename) | ||
- | sheet_info = None | ||
- | sheet_cat = None | ||
- | sheet_products = None | ||
for sheet in xlsfile.sheets(): | for sheet in xlsfile.sheets(): | ||
- | | + | |
- | sheet_info | + | for curr_row in range(sheet.nrows): |
- | elif sheet.name == u'Danh m\u1ee5c': | + | curr_cell = 0 |
- | sheet_cat = sheet | + | while curr_cell < n_cells: |
- | elif sheet.name == u' | + | cell_type = sheet.cell_type(curr_row, |
- | sheet_products = sheet | + | if cell_type == xlrd.XL_CELL_EMPTY: |
- | ''' | + | curr_cell += 1 |
- | n_cells = sheet_info.ncols | + | continue |
- | for curr_row in range(sheet_info.nrows): | + | value = sheet.cell_value(curr_row, |
- | curr_cell = 0 | + | |
- | while curr_cell < n_cells: | + | |
- | cell_type = sheet_info.cell_type(curr_row, | + | |
- | if cell_type == xlrd.XL_CELL_EMPTY: | + | |
curr_cell += 1 | curr_cell += 1 | ||
- | | + | |
- | | + | parsexls(constantsfile) |
- | if value == u' | + | |
- | curr_cell += 1 | + | |
- | shop_info[' | + | |
- | elif value == u' | + | |
- | curr_cell += 1 | + | |
- | shop_info[' | + | |
- | elif value == u' | + | |
- | curr_cell += 1 | + | |
- | shop_info[' | + | |
- | elif value == u' | + | |
- | curr_cell += 1 | + | |
- | shop_info[' | + | |
- | elif value == u' | + | |
- | curr_cell += 1 | + | |
- | shop_info[' | + | |
- | curr_cell += 1 | + | |
- | ''' | + | |
- | n_cells = sheet_cat.ncols | + | |
- | for curr_row in range(sheet_cat.nrows): | + | |
- | curr_cell = 0 | + | |
- | while curr_cell < n_cells: | + | |
- | cell_type = sheet_cat.cell_type(curr_row, | + | |
- | if cell_type == xlrd.XL_CELL_EMPTY: | + | |
- | curr_cell += 1 | + | |
- | continue | + | |
- | value = sheet_cat.cell_value(curr_row, | + | |
- | if value != u' | + | |
- | shop_cat.append(value) | + | |
- | curr_cell += 1 | + | |
- | ''' | + | |
- | n_cells = sheet_products.ncols | + | |
- | first_data_col = -1 | + | |
- | is_first_data_row = True | + | |
- | for curr_row in range(sheet_products.nrows): | + | |
- | curr_cell = 0 | + | |
- | while curr_cell < n_cells: | + | |
- | cell_type = sheet_products.cell_type(curr_row, | + | |
- | if cell_type == xlrd.XL_CELL_EMPTY: | + | |
- | curr_cell += 1 | + | |
- | continue | + | |
- | if first_data_col < 0: | + | |
- | first_data_col = curr_cell | + | |
- | data_col = curr_cell - first_data_col | + | |
- | value = sheet_products.cell_value(curr_row, | + | |
- | if data_col == 0: | + | |
- | product_info = {} | + | |
- | product_info[' | + | |
- | elif data_col == 1: | + | |
- | product_info[' | + | |
- | elif data_col == 2: | + | |
- | if cell_type == xlrd.XL_CELL_NUMBER: | + | |
- | value = int(value) | + | |
- | product_info[' | + | |
- | elif data_col == 3: | + | |
- | product_info[' | + | |
- | elif data_col == 4: | + | |
- | product_info[' | + | |
- | elif data_col == 5: | + | |
- | product_info[' | + | |
- | elif data_col == 6: | + | |
- | product_info[' | + | |
- | if is_first_data_row: | + | |
- | is_first_data_row = False | + | |
- | else: | + | |
- | shop_products.append(product_info) | + | |
- | curr_cell += 1 | + | |
- | return shop_info, shop_cat, shop_products | + | |
- | if __name__ == ' | + | |
- | shop_info, shop_cat, shop_products = parseshopxls(' | + | |
- | print shop_info | + | |
- | print shop_cat | + | |
- | for product in shop_products: | + | |
- | print ' | + | |
- | print product | + | |
</ | </ | ||
+ | ==== Error Parse UTF-8 file ==== | ||
+ | < | ||
+ | UnicodeDecodeError: | ||
+ | </ | ||
+ | You can debug for information:< | ||
+ | xlsfile = xlrd.open_workbook(filename, | ||
+ | </ | ||
+ | BOF: op=0x0809 vers=0x0600 stream=0x0005 buildid=14420 buildyr=1997 -> BIFF80 | ||
+ | CODEPAGE: codepage 1200 -> encoding ' | ||
+ | DATEMODE: datemode 0 | ||
+ | </ | ||
+ | |||
+ | **Fix**: Lỗi do file exel, mở lại file exel và lưu lại sẽ hết lỗi | ||
+ | ===== Using xlwt ===== |
python/exelprocess.1579447224.txt.gz · Last modified: 2022/10/29 16:15 (external edit)