def print_tests(url, grid): print(' url={}'.format(url)) print(' rows={}'.format(grid.height)) print(' cols={}'.format(grid.width)) if any( isinstance(e, BackgroundElt) and e.color != 0xffffff for r, c, e in grid.features): print(" fill='''") print(grid_to_string(grid)) print("'''") if any(isinstance(e, BorderElt) for r, c, e in grid.features): bordered = Grid(grid.height, grid.width) bordered.features.extend(grid.features) bordered.features.extend(outside_bars(grid)) print(" bars='''") print(bars_to_string(bordered)) print("'''") if any(isinstance(e, TextElt) for r, c, e in grid.features): label_str = labels_to_string(grid) gr = Grid(grid.height, grid.width) gr.features.extend(autonumber(gr)) if label_str == labels_to_string(grid): print(" cells_with_text='auto'") else: print(" cells_with_text='''") print(label_str) print("'''")
def test_all(self): detected=(len(self.img.breaks[0])-1,len(self.img.breaks[1])-1) expected=(self.rows,self.cols) self.assertEqual(expected,detected,'wrong dimensions') if hasattr(self,'fill'): with self.subTest('fill'): grid=self.img.grid() grid.features.extend(self.img.read_background()) f=grid_to_string(grid) self.assertEqual(self.fill.strip(),f.strip()) if hasattr(self,'bars'): with self.subTest('bars'): grid=self.img.grid() grid.features.extend(self.img.read_bars()) grid.features.extend(outside_bars(grid)) b=bars_to_string(grid) self.assertEqual(self.bars.strip(),b.strip()) if hasattr(self,'cells_with_text'): with self.subTest('cells_with_text'): if self.cells_with_text=='auto': grid=self.img.grid() grid.features.extend(self.img.read_background()) grid.features.extend(self.img.read_bars()) grid.features.extend(autonumber(grid)) self.cells_with_text=labels_to_string(grid) grid=self.img.grid() grid.features.extend(self.img.autonumber_if_text_found()) t=labels_to_string(grid) self.assertEqual(self.cells_with_text.strip(),t.strip())
def test_all(self): detected = (len(self.img.breaks[0]) - 1, len(self.img.breaks[1]) - 1) expected = (self.rows, self.cols) self.assertEqual(expected, detected, 'wrong dimensions') if hasattr(self, 'fill'): with self.subTest('fill'): grid = self.img.grid() grid.features.extend(self.img.read_background()) f = grid_to_string(grid) self.assertEqual(self.fill.strip(), f.strip()) if hasattr(self, 'bars'): with self.subTest('bars'): grid = self.img.grid() grid.features.extend(self.img.read_bars()) grid.features.extend(outside_bars(grid)) b = bars_to_string(grid) self.assertEqual(self.bars.strip(), b.strip()) if hasattr(self, 'cells_with_text'): with self.subTest('cells_with_text'): if self.cells_with_text == 'auto': grid = self.img.grid() grid.features.extend(self.img.read_background()) grid.features.extend(self.img.read_bars()) grid.features.extend(autonumber(grid)) self.cells_with_text = labels_to_string(grid) grid = self.img.grid() grid.features.extend(self.img.autonumber_if_text_found()) t = labels_to_string(grid) self.assertEqual(self.cells_with_text.strip(), t.strip())
def process(grid,args): if args.print_testdata: from cross2sheet.test.test import print_tests print_tests(grid) if args.autonumber: grid.features.extend(autonumber(grid)) if args.outer_border: grid.features.extend(outside_bars(grid)) pad(grid,*args.padding)
def the_wicked_switch(): req=urlopen('http://web.mit.edu/puzzle/www/2012/puzzles/a_circus_line/the_wicked_switch/1.png') data=req.read() req.close() img=ImageGrid(data) grid=img.grid() grid.features.extend(img.read_background()) grid.features.extend(img.read_text_ocr()) grid.features.extend(outside_bars(grid)) save_xlsx(grid,'the_wicked_switch.xlsx')
def process(grid, args): if args.print_testdata: from cross2sheet.test.test import print_tests url = args.input_file_or_url url = url.replace('http://web.mit.edu/puzzle/www/', '') print_tests(url, grid) if args.autonumber: grid.features.extend(autonumber(grid)) if args.outer_border: grid.features.extend(outside_bars(grid)) pad(grid, *args.padding)
def a_puzzle_with_answer_nowhere_man(): req=urlopen('http://web.mit.edu/puzzle/www/2014/puzzle/puzzle_with_answer_nowhere_man/grid.png') data=req.read() req.close() img=ImageGrid(data) grid=img.grid() grid.features.extend(img.read_background()) grid.features.extend(img.read_bars()) grid.features.extend(outside_bars(grid)) grid.features.extend(autonumber(grid)) save_xlsx(grid,'nowhere_man.xlsx')
def form_data_to_excel(form): td=TableData.from_json(form['data']) g=Grid(td.height,td.width) if 'back' in form: g.features.extend(td.back) if 'bar' in form: g.features.extend(td.bars) if form['auto']=='auto': g.features.extend(autonumber(g)) elif form['auto']=='text': g.features.extend(td.text) g.features.extend(outside_bars(g)) i=BytesIO() to_openpyxl(g,text_in_cells='cells' in form,text_in_comments='comments' in form).save(i) i.seek(0) return i
def print_tests(grid): print('\trows={}'.format(grid.height)) print('\tcols={}'.format(grid.width)) if any(isinstance(e,BackgroundElt) and e.color!=0xffffff for r,c,e in grid.features): print("\tfill='''") print(grid_to_string(grid)) print("'''") if any(isinstance(e,BorderElt) for r,c,e in grid.features): bordered=Grid(grid.height,grid.width) bordered.features.extend(grid.features) bordered.features.extend(outside_bars(grid)) print("\tbars='''") print(bars_to_string(bordered)) print("'''") if any(isinstance(e,TextElt) for r,c,e in grid.features): print("\tcells_with_text='''") print(labels_to_string(grid)) print("'''")
def form_data_to_excel(form): td = TableData.from_json(form['data']) g = Grid(td.height, td.width) if 'back' in form: g.features.extend(td.back) if 'bar' in form: g.features.extend(td.bars) if form['auto'] == 'text': g.features.extend(td.text) g.validate() if form['auto'] == 'auto': g.features.extend(autonumber(g)) g.features.extend(outside_bars(g)) i = BytesIO() to_openpyxl(g, text_in_cells='cells' in form, text_in_comments='comments' in form).save(i) i.seek(0) return i