worksheet.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. # Copyright (c) 2010-2019 openpyxl
  2. """Worksheet is the 2nd-level container in Excel."""
  3. # Python stdlib imports
  4. from itertools import islice, product, chain
  5. from operator import itemgetter
  6. from inspect import isgenerator
  7. # compatibility imports
  8. from openpyxl.compat import (
  9. deprecated,
  10. )
  11. try:
  12. range = xrange
  13. except NameError:
  14. pass
  15. # package imports
  16. from openpyxl.utils import (
  17. column_index_from_string,
  18. get_column_letter,
  19. range_boundaries,
  20. coordinate_to_tuple,
  21. absolute_coordinate,
  22. )
  23. from openpyxl.cell import Cell, MergedCell
  24. from openpyxl.formatting.formatting import ConditionalFormattingList
  25. from openpyxl.packaging.relationship import RelationshipList
  26. from openpyxl.workbook.child import _WorkbookChild
  27. from openpyxl.workbook.defined_name import COL_RANGE_RE, ROW_RANGE_RE
  28. from openpyxl.formula.translate import Translator
  29. from .datavalidation import DataValidationList
  30. from .page import (
  31. PrintPageSetup,
  32. PageMargins,
  33. PrintOptions,
  34. )
  35. from .dimensions import (
  36. ColumnDimension,
  37. RowDimension,
  38. DimensionHolder,
  39. SheetFormatProperties,
  40. )
  41. from .protection import SheetProtection
  42. from .filters import AutoFilter
  43. from .views import (
  44. Pane,
  45. Selection,
  46. SheetViewList,
  47. )
  48. from .cell_range import MultiCellRange, CellRange
  49. from .merge import MergedCellRange
  50. from .properties import WorksheetProperties
  51. from .pagebreak import RowBreak, ColBreak
  52. from .scenario import ScenarioList
  53. class Worksheet(_WorkbookChild):
  54. """Represents a worksheet.
  55. Do not create worksheets yourself,
  56. use :func:`openpyxl.workbook.Workbook.create_sheet` instead
  57. """
  58. _rel_type = "worksheet"
  59. _path = "/xl/worksheets/sheet{0}.xml"
  60. mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"
  61. BREAK_NONE = 0
  62. BREAK_ROW = 1
  63. BREAK_COLUMN = 2
  64. SHEETSTATE_VISIBLE = 'visible'
  65. SHEETSTATE_HIDDEN = 'hidden'
  66. SHEETSTATE_VERYHIDDEN = 'veryHidden'
  67. # Paper size
  68. PAPERSIZE_LETTER = '1'
  69. PAPERSIZE_LETTER_SMALL = '2'
  70. PAPERSIZE_TABLOID = '3'
  71. PAPERSIZE_LEDGER = '4'
  72. PAPERSIZE_LEGAL = '5'
  73. PAPERSIZE_STATEMENT = '6'
  74. PAPERSIZE_EXECUTIVE = '7'
  75. PAPERSIZE_A3 = '8'
  76. PAPERSIZE_A4 = '9'
  77. PAPERSIZE_A4_SMALL = '10'
  78. PAPERSIZE_A5 = '11'
  79. # Page orientation
  80. ORIENTATION_PORTRAIT = 'portrait'
  81. ORIENTATION_LANDSCAPE = 'landscape'
  82. def __init__(self, parent, title=None):
  83. _WorkbookChild.__init__(self, parent, title)
  84. self._setup()
  85. def _setup(self):
  86. self.row_dimensions = DimensionHolder(worksheet=self,
  87. default_factory=self._add_row)
  88. self.column_dimensions = DimensionHolder(worksheet=self,
  89. default_factory=self._add_column)
  90. self.row_breaks = RowBreak()
  91. self.col_breaks = ColBreak()
  92. self.page_breaks = (self.row_breaks, self.col_breaks)
  93. self._cells = {}
  94. self._charts = []
  95. self._images = []
  96. self._rels = RelationshipList()
  97. self._drawing = None
  98. self._comments = []
  99. self.merged_cells = MultiCellRange()
  100. self._tables = []
  101. self._pivots = []
  102. self.data_validations = DataValidationList()
  103. self._hyperlinks = []
  104. self.sheet_state = 'visible'
  105. self.page_setup = PrintPageSetup(worksheet=self)
  106. self.print_options = PrintOptions()
  107. self._print_rows = None
  108. self._print_cols = None
  109. self._print_area = None
  110. self.page_margins = PageMargins()
  111. self.views = SheetViewList()
  112. self.protection = SheetProtection()
  113. self._current_row = 0
  114. self.auto_filter = AutoFilter()
  115. self.paper_size = None
  116. self.formula_attributes = {}
  117. self.orientation = None
  118. self.conditional_formatting = ConditionalFormattingList()
  119. self.legacy_drawing = None
  120. self.sheet_properties = WorksheetProperties()
  121. self.sheet_format = SheetFormatProperties()
  122. self.scenarios = ScenarioList()
  123. @property
  124. def sheet_view(self):
  125. return self.views.sheetView[0]
  126. @property
  127. def selected_cell(self):
  128. return self.sheet_view.selection[0].sqref
  129. @property
  130. def active_cell(self):
  131. return self.sheet_view.selection[0].activeCell
  132. @property
  133. def show_gridlines(self):
  134. return self.sheet_view.showGridLines
  135. """ To keep compatibility with previous versions"""
  136. @property
  137. def show_summary_below(self):
  138. return self.sheet_properties.outlinePr.summaryBelow
  139. @property
  140. def show_summary_right(self):
  141. return self.sheet_properties.outlinePr.summaryRight
  142. @property
  143. def freeze_panes(self):
  144. if self.sheet_view.pane is not None:
  145. return self.sheet_view.pane.topLeftCell
  146. @freeze_panes.setter
  147. def freeze_panes(self, topLeftCell=None):
  148. if isinstance(topLeftCell, Cell):
  149. topLeftCell = topLeftCell.coordinate
  150. if topLeftCell == 'A1':
  151. topLeftCell = None
  152. if not topLeftCell:
  153. self.sheet_view.pane = None
  154. return
  155. row, column = coordinate_to_tuple(topLeftCell)
  156. view = self.sheet_view
  157. view.pane = Pane(topLeftCell=topLeftCell,
  158. activePane="topRight",
  159. state="frozen")
  160. view.selection[0].pane = "topRight"
  161. if column > 1:
  162. view.pane.xSplit = column - 1
  163. if row > 1:
  164. view.pane.ySplit = row - 1
  165. view.pane.activePane = 'bottomLeft'
  166. view.selection[0].pane = "bottomLeft"
  167. if column > 1:
  168. view.selection[0].pane = "bottomRight"
  169. view.pane.activePane = 'bottomRight'
  170. if row > 1 and column > 1:
  171. sel = list(view.selection)
  172. sel.insert(0, Selection(pane="topRight", activeCell=None, sqref=None))
  173. sel.insert(1, Selection(pane="bottomLeft", activeCell=None, sqref=None))
  174. view.selection = sel
  175. def cell(self, row, column, value=None):
  176. """
  177. Returns a cell object based on the given coordinates.
  178. Usage: cell(row=15, column=1, value=5)
  179. Calling `cell` creates cells in memory when they
  180. are first accessed.
  181. :param row: row index of the cell (e.g. 4)
  182. :type row: int
  183. :param column: column index of the cell (e.g. 3)
  184. :type column: int
  185. :param value: value of the cell (e.g. 5)
  186. :type value: numeric or time or string or bool or none
  187. :rtype: openpyxl.cell.cell.Cell
  188. """
  189. if row < 1 or column < 1:
  190. raise ValueError("Row or column values must be at least 1")
  191. cell = self._get_cell(row, column)
  192. if value is not None:
  193. cell.value = value
  194. return cell
  195. def _get_cell(self, row, column):
  196. """
  197. Internal method for getting a cell from a worksheet.
  198. Will create a new cell if one doesn't already exist.
  199. """
  200. coordinate = (row, column)
  201. if not coordinate in self._cells:
  202. cell = Cell(self, row=row, column=column)
  203. self._add_cell(cell)
  204. return self._cells[coordinate]
  205. def _add_cell(self, cell):
  206. """
  207. Internal method for adding cell objects.
  208. """
  209. column = cell.col_idx
  210. row = cell.row
  211. self._current_row = max(row, self._current_row)
  212. self._cells[(row, column)] = cell
  213. def __getitem__(self, key):
  214. """Convenience access by Excel style coordinates
  215. The key can be a single cell coordinate 'A1', a range of cells 'A1:D25',
  216. individual rows or columns 'A', 4 or ranges of rows or columns 'A:D',
  217. 4:10.
  218. Single cells will always be created if they do not exist.
  219. Returns either a single cell or a tuple of rows or columns.
  220. """
  221. if isinstance(key, slice):
  222. if not all([key.start, key.stop]):
  223. raise IndexError("{0} is not a valid coordinate or range".format(key))
  224. key = "{0}:{1}".format(key.start, key.stop)
  225. if isinstance(key, int):
  226. key = str(key
  227. )
  228. min_col, min_row, max_col, max_row = range_boundaries(key)
  229. if not any([min_col, min_row, max_col, max_row]):
  230. raise IndexError("{0} is not a valid coordinate or range".format(key))
  231. if not min_row:
  232. cols = tuple(self.iter_cols(min_col, max_col))
  233. if min_col == max_col:
  234. cols = cols[0]
  235. return cols
  236. if not min_col:
  237. rows = tuple(self.iter_rows(min_col=min_col, min_row=min_row,
  238. max_col=self.max_column, max_row=max_row))
  239. if min_row == max_row:
  240. rows = rows[0]
  241. return rows
  242. if ":" not in key:
  243. return self._get_cell(min_row, min_col)
  244. return tuple(self.iter_rows(min_row=min_row, min_col=min_col,
  245. max_row=max_row, max_col=max_col))
  246. def __setitem__(self, key, value):
  247. self[key].value = value
  248. def __iter__(self):
  249. return self.iter_rows()
  250. def __delitem__(self, key):
  251. row, column = coordinate_to_tuple(key)
  252. if (row, column) in self._cells:
  253. del self._cells[(row, column)]
  254. @property
  255. def min_row(self):
  256. """The minimium row index containing data (1-based)
  257. :type: int
  258. """
  259. min_row = 1
  260. if self._cells:
  261. rows = set(c[0] for c in self._cells)
  262. min_row = min(rows)
  263. return min_row
  264. @property
  265. def max_row(self):
  266. """The maximum row index containing data (1-based)
  267. :type: int
  268. """
  269. max_row = 1
  270. if self._cells:
  271. rows = set(c[0] for c in self._cells)
  272. max_row = max(rows)
  273. return max_row
  274. @property
  275. def min_column(self):
  276. """The minimum column index containing data (1-based)
  277. :type: int
  278. """
  279. min_col = 1
  280. if self._cells:
  281. cols = set(c[1] for c in self._cells)
  282. min_col = min(cols)
  283. return min_col
  284. @property
  285. def max_column(self):
  286. """The maximum column index containing data (1-based)
  287. :type: int
  288. """
  289. max_col = 1
  290. if self._cells:
  291. cols = set(c[1] for c in self._cells)
  292. max_col = max(cols)
  293. return max_col
  294. def calculate_dimension(self):
  295. """Return the minimum bounding range for all cells containing data (ex. 'A1:M24')
  296. :rtype: string
  297. """
  298. if self._cells:
  299. rows = set()
  300. cols = set()
  301. for row, col in self._cells:
  302. rows.add(row)
  303. cols.add(col)
  304. max_row = max(rows)
  305. max_col = max(cols)
  306. min_col = min(cols)
  307. min_row = min(rows)
  308. else:
  309. return "A1:A1"
  310. return '%s%d:%s%d' % (
  311. get_column_letter(min_col), min_row,
  312. get_column_letter(max_col), max_row
  313. )
  314. @property
  315. def dimensions(self):
  316. """Returns the result of :func:`calculate_dimension`"""
  317. return self.calculate_dimension()
  318. def iter_rows(self, min_row=None, max_row=None, min_col=None, max_col=None, values_only=False):
  319. """
  320. Produces cells from the worksheet, by row. Specify the iteration range
  321. using indices of rows and columns.
  322. If no indices are specified the range starts at A1.
  323. If no cells are in the worksheet an empty tuple will be returned.
  324. :param min_col: smallest column index (1-based index)
  325. :type min_col: int
  326. :param min_row: smallest row index (1-based index)
  327. :type min_row: int
  328. :param max_col: largest column index (1-based index)
  329. :type max_col: int
  330. :param max_row: largest row index (1-based index)
  331. :type max_row: int
  332. :param values_only: whether only cell values should be returned
  333. :type values_only: bool
  334. :rtype: generator
  335. """
  336. if self._current_row == 0 and not any([min_col, min_row, max_col, max_row ]):
  337. return ()
  338. min_col = min_col or 1
  339. min_row = min_row or 1
  340. max_col = max_col or self.max_column
  341. max_row = max_row or self.max_row
  342. return self._cells_by_row(min_col, min_row, max_col, max_row, values_only)
  343. def _cells_by_row(self, min_col, min_row, max_col, max_row, values_only=False):
  344. for row in range(min_row, max_row + 1):
  345. cells = (self.cell(row=row, column=column) for column in range(min_col, max_col + 1))
  346. if values_only:
  347. yield tuple(cell.value for cell in cells)
  348. else:
  349. yield tuple(cells)
  350. @property
  351. def rows(self):
  352. """Produces all cells in the worksheet, by row (see :func:`iter_rows`)
  353. :type: generator
  354. """
  355. return self.iter_rows()
  356. @property
  357. def values(self):
  358. """Produces all cell values in the worksheet, by row
  359. :type: generator
  360. """
  361. for row in self.iter_rows(values_only=True):
  362. yield row
  363. def iter_cols(self, min_col=None, max_col=None, min_row=None, max_row=None, values_only=False):
  364. """
  365. Produces cells from the worksheet, by column. Specify the iteration range
  366. using indices of rows and columns.
  367. If no indices are specified the range starts at A1.
  368. If no cells are in the worksheet an empty tuple will be returned.
  369. :param min_col: smallest column index (1-based index)
  370. :type min_col: int
  371. :param min_row: smallest row index (1-based index)
  372. :type min_row: int
  373. :param max_col: largest column index (1-based index)
  374. :type max_col: int
  375. :param max_row: largest row index (1-based index)
  376. :type max_row: int
  377. :param values_only: whether only cell values should be returned
  378. :type values_only: bool
  379. :rtype: generator
  380. """
  381. if self._current_row == 0 and not any([min_col, min_row, max_col, max_row]):
  382. return ()
  383. min_col = min_col or 1
  384. min_row = min_row or 1
  385. max_col = max_col or self.max_column
  386. max_row = max_row or self.max_row
  387. return self._cells_by_col(min_col, min_row, max_col, max_row, values_only)
  388. def _cells_by_col(self, min_col, min_row, max_col, max_row, values_only=False):
  389. """
  390. Get cells by column
  391. """
  392. for column in range(min_col, max_col+1):
  393. cells = (self.cell(row=row, column=column)
  394. for row in range(min_row, max_row+1))
  395. if values_only:
  396. yield tuple(cell.value for cell in cells)
  397. else:
  398. yield tuple(cells)
  399. @property
  400. def columns(self):
  401. """Produces all cells in the worksheet, by column (see :func:`iter_cols`)"""
  402. return self.iter_cols()
  403. def set_printer_settings(self, paper_size, orientation):
  404. """Set printer settings """
  405. self.page_setup.paperSize = paper_size
  406. self.page_setup.orientation = orientation
  407. def add_data_validation(self, data_validation):
  408. """ Add a data-validation object to the sheet. The data-validation
  409. object defines the type of data-validation to be applied and the
  410. cell or range of cells it should apply to.
  411. """
  412. self.data_validations.append(data_validation)
  413. def add_chart(self, chart, anchor=None):
  414. """
  415. Add a chart to the sheet
  416. Optionally provide a cell for the top-left anchor
  417. """
  418. if anchor is not None:
  419. chart.anchor = anchor
  420. self._charts.append(chart)
  421. def add_image(self, img, anchor=None):
  422. """
  423. Add an image to the sheet.
  424. Optionally provide a cell for the top-left anchor
  425. """
  426. if anchor is not None:
  427. img.anchor = anchor
  428. self._images.append(img)
  429. def add_table(self, table):
  430. self._tables.append(table)
  431. def add_pivot(self, pivot):
  432. self._pivots.append(pivot)
  433. def merge_cells(self, range_string=None, start_row=None, start_column=None, end_row=None, end_column=None):
  434. """ Set merge on a cell range. Range is a cell range (e.g. A1:E1) """
  435. cr = CellRange(range_string=range_string, min_col=start_column, min_row=start_row,
  436. max_col=end_column, max_row=end_row)
  437. self.merged_cells.add(cr)
  438. self._clean_merge_range(cr)
  439. def _clean_merge_range(self, cr):
  440. """
  441. Remove all but the top left-cell from a range of merged cells
  442. and recreate the lost border information.
  443. Borders are then applied
  444. """
  445. mcr = MergedCellRange(self, cr.coord)
  446. cells = chain.from_iterable(mcr.rows)
  447. next(cells) # skip first cell
  448. for row, col in cells:
  449. self._cells[row, col] = MergedCell(self, row, col)
  450. mcr.format()
  451. @property
  452. @deprecated("Use ws.merged_cells.ranges")
  453. def merged_cell_ranges(self):
  454. """Return a copy of cell ranges"""
  455. return self.merged_cells.ranges[:]
  456. def unmerge_cells(self, range_string=None, start_row=None, start_column=None, end_row=None, end_column=None):
  457. """ Remove merge on a cell range. Range is a cell range (e.g. A1:E1) """
  458. cr = CellRange(range_string=range_string, min_col=start_column, min_row=start_row,
  459. max_col=end_column, max_row=end_row)
  460. if cr.coord not in self.merged_cells:
  461. raise ValueError("Cell range {0} is not merged".format(cr.coord))
  462. self.merged_cells.remove(cr)
  463. cells = chain.from_iterable(cr.rows)
  464. next(cells) # skip first cell
  465. for row, col in cells:
  466. del self._cells[(row, col)]
  467. def append(self, iterable):
  468. """Appends a group of values at the bottom of the current sheet.
  469. * If it's a list: all values are added in order, starting from the first column
  470. * If it's a dict: values are assigned to the columns indicated by the keys (numbers or letters)
  471. :param iterable: list, range or generator, or dict containing values to append
  472. :type iterable: list|tuple|range|generator or dict
  473. Usage:
  474. * append(['This is A1', 'This is B1', 'This is C1'])
  475. * **or** append({'A' : 'This is A1', 'C' : 'This is C1'})
  476. * **or** append({1 : 'This is A1', 3 : 'This is C1'})
  477. :raise: TypeError when iterable is neither a list/tuple nor a dict
  478. """
  479. row_idx = self._current_row + 1
  480. if (isinstance(iterable, (list, tuple, range))
  481. or isgenerator(iterable)):
  482. for col_idx, content in enumerate(iterable, 1):
  483. if isinstance(content, Cell):
  484. # compatible with write-only mode
  485. cell = content
  486. if cell.parent and cell.parent != self:
  487. raise ValueError("Cells cannot be copied from other worksheets")
  488. cell.parent = self
  489. cell.column = col_idx
  490. cell.row = row_idx
  491. else:
  492. cell = Cell(self, row=row_idx, column=col_idx, value=content)
  493. self._cells[(row_idx, col_idx)] = cell
  494. elif isinstance(iterable, dict):
  495. for col_idx, content in iterable.items():
  496. if isinstance(col_idx, str):
  497. col_idx = column_index_from_string(col_idx)
  498. cell = Cell(self, row=row_idx, column=col_idx, value=content)
  499. self._cells[(row_idx, col_idx)] = cell
  500. else:
  501. self._invalid_row(iterable)
  502. self._current_row = row_idx
  503. def _move_cells(self, min_row=None, min_col=None, offset=0, row_or_col="row"):
  504. """
  505. Move either rows or columns around by the offset
  506. """
  507. reverse = offset > 0 # start at the end if inserting
  508. row_offset = 0
  509. col_offset = 0
  510. # need to make affected ranges contiguous
  511. if row_or_col == 'row':
  512. cells = self.iter_rows(min_row=min_row)
  513. row_offset = offset
  514. key = 0
  515. else:
  516. cells = self.iter_cols(min_col=min_col)
  517. col_offset = offset
  518. key = 1
  519. cells = list(cells)
  520. for row, column in sorted(self._cells, key=itemgetter(key), reverse=reverse):
  521. if min_row and row < min_row:
  522. continue
  523. elif min_col and column < min_col:
  524. continue
  525. self._move_cell(row, column, row_offset, col_offset)
  526. def insert_rows(self, idx, amount=1):
  527. """
  528. Insert row or rows before row==idx
  529. """
  530. self._move_cells(min_row=idx, offset=amount, row_or_col="row")
  531. self._current_row = self.max_row
  532. def insert_cols(self, idx, amount=1):
  533. """
  534. Insert column or columns before col==idx
  535. """
  536. self._move_cells(min_col=idx, offset=amount, row_or_col="column")
  537. def delete_rows(self, idx, amount=1):
  538. """
  539. Delete row or rows from row==idx
  540. """
  541. remainder = _gutter(idx, amount, self.max_row)
  542. self._move_cells(min_row=idx+amount, offset=-amount, row_or_col="row")
  543. # calculating min and max col is an expensive operation, do it only once
  544. min_col = self.min_column
  545. max_col = self.max_column + 1
  546. for row in remainder:
  547. for col in range(min_col, max_col):
  548. if (row, col) in self._cells:
  549. del self._cells[row, col]
  550. self._current_row = self.max_row
  551. if not self._cells:
  552. self._current_row = 0
  553. def delete_cols(self, idx, amount=1):
  554. """
  555. Delete column or columns from col==idx
  556. """
  557. remainder = _gutter(idx, amount, self.max_column)
  558. self._move_cells(min_col=idx+amount, offset=-amount, row_or_col="column")
  559. # calculating min and max row is an expensive operation, do it only once
  560. min_row = self.min_row
  561. max_row = self.max_row + 1
  562. for col in remainder:
  563. for row in range(min_row, max_row):
  564. if (row, col) in self._cells:
  565. del self._cells[row, col]
  566. def move_range(self, cell_range, rows=0, cols=0, translate=False):
  567. """
  568. Move a cell range by the number of rows and/or columns:
  569. down if rows > 0 and up if rows < 0
  570. right if cols > 0 and left if cols < 0
  571. Existing cells will be overwritten.
  572. Formulae and references will not be updated.
  573. """
  574. if isinstance(cell_range, str):
  575. cell_range = CellRange(cell_range)
  576. if not isinstance(cell_range, CellRange):
  577. raise ValueError("Only CellRange objects can be moved")
  578. if not rows and not cols:
  579. return
  580. down = rows > 0
  581. right = cols > 0
  582. if rows:
  583. cells = sorted(cell_range.rows, reverse=down)
  584. else:
  585. cells = sorted(cell_range.cols, reverse=right)
  586. for row, col in chain.from_iterable(cells):
  587. self._move_cell(row, col, rows, cols, translate)
  588. # rebase moved range
  589. cell_range.shift(row_shift=rows, col_shift=cols)
  590. def _move_cell(self, row, column, row_offset, col_offset, translate=False):
  591. """
  592. Move a cell from one place to another.
  593. Delete at old index
  594. Rebase coordinate
  595. """
  596. cell = self._get_cell(row, column)
  597. new_row = cell.row + row_offset
  598. new_col = cell.column + col_offset
  599. self._cells[new_row, new_col] = cell
  600. del self._cells[(cell.row, cell.column)]
  601. cell.row = new_row
  602. cell.column = new_col
  603. if translate and cell.data_type == "f":
  604. t = Translator(cell.value, cell.coordinate)
  605. cell.value = t.translate_formula(row_delta=row_offset, col_delta=col_offset)
  606. def _invalid_row(self, iterable):
  607. raise TypeError('Value must be a list, tuple, range or generator, or a dict. Supplied value is {0}'.format(
  608. type(iterable))
  609. )
  610. def _add_column(self):
  611. """Dimension factory for column information"""
  612. return ColumnDimension(self)
  613. def _add_row(self):
  614. """Dimension factory for row information"""
  615. return RowDimension(self)
  616. @property
  617. def print_title_rows(self):
  618. """Rows to be printed at the top of every page (ex: '1:3')"""
  619. if self._print_rows:
  620. return self._print_rows
  621. @print_title_rows.setter
  622. def print_title_rows(self, rows):
  623. """
  624. Set rows to be printed on the top of every page
  625. format `1:3`
  626. """
  627. if rows is not None:
  628. if not ROW_RANGE_RE.match(rows):
  629. raise ValueError("Print title rows must be the form 1:3")
  630. self._print_rows = rows
  631. @property
  632. def print_title_cols(self):
  633. """Columns to be printed at the left side of every page (ex: 'A:C')"""
  634. if self._print_cols:
  635. return self._print_cols
  636. @print_title_cols.setter
  637. def print_title_cols(self, cols):
  638. """
  639. Set cols to be printed on the left of every page
  640. format ``A:C`
  641. """
  642. if cols is not None:
  643. if not COL_RANGE_RE.match(cols):
  644. raise ValueError("Print title cols must be the form C:D")
  645. self._print_cols = cols
  646. @property
  647. def print_titles(self):
  648. if self.print_title_cols and self.print_title_rows:
  649. return ",".join([self.print_title_rows, self.print_title_cols])
  650. else:
  651. return self.print_title_rows or self.print_title_cols
  652. @property
  653. def print_area(self):
  654. """
  655. The print area for the worksheet, or None if not set. To set, supply a range
  656. like 'A1:D4' or a list of ranges.
  657. """
  658. return self._print_area
  659. @print_area.setter
  660. def print_area(self, value):
  661. """
  662. Range of cells in the form A1:D4 or list of ranges
  663. """
  664. if isinstance(value, str):
  665. value = [value]
  666. self._print_area = [absolute_coordinate(v) for v in value]
  667. def _gutter(idx, offset, max_val):
  668. """
  669. When deleting rows and columns are deleted we rely on overwriting.
  670. This may not be the case for a large offset on small set of cells:
  671. range(cells_to_delete) > range(cell_to_be_moved)
  672. """
  673. gutter = range(max(max_val+1-offset, idx), min(idx+offset, max_val)+1)
  674. return gutter