Worksheet.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. # -*- coding: windows-1252 -*-
  2. # BOF
  3. # UNCALCED
  4. # INDEX
  5. # Calculation Settings Block
  6. # PRINTHEADERS
  7. # PRINTGRIDLINES
  8. # GRIDSET
  9. # GUTS
  10. # DEFAULTROWHEIGHT
  11. # WSBOOL
  12. # Page Settings Block
  13. # Worksheet Protection Block
  14. # DEFCOLWIDTH
  15. # COLINFO
  16. # SORT
  17. # DIMENSIONS
  18. # Row Blocks
  19. # WINDOW2
  20. # SCL
  21. # PANE
  22. # SELECTION
  23. # STANDARDWIDTH
  24. # MERGEDCELLS
  25. # LABELRANGES
  26. # PHONETIC
  27. # Conditional Formatting Table
  28. # Hyperlink Table
  29. # Data Validity Table
  30. # SHEETLAYOUT (BIFF8X only)
  31. # SHEETPROTECTION (BIFF8X only)
  32. # RANGEPROTECTION (BIFF8X only)
  33. # EOF
  34. from . import BIFFRecords
  35. from . import Bitmap
  36. from . import Style
  37. from .Row import Row
  38. from .Column import Column
  39. from .compat import unicode, itervalues
  40. import tempfile
  41. class Worksheet(object):
  42. """
  43. This is a class
  44. representing the contents of a sheet in a workbook.
  45. .. warning::
  46. You don't normally create instances of this class yourself.
  47. They are returned from calls to :meth:`~xlwt.Workbook.Workbook.add_sheet`.
  48. """
  49. # a safe default value, 3 is always valid!
  50. active_pane = 3
  51. #################################################################
  52. ## Constructor
  53. #################################################################
  54. def __init__(self, sheetname, parent_book, cell_overwrite_ok=False):
  55. self.Row = Row
  56. self.Column = Column
  57. self.__name = sheetname
  58. self.__parent = parent_book
  59. self._cell_overwrite_ok = cell_overwrite_ok
  60. self.__rows = {}
  61. self.__cols = {}
  62. self.__merged_ranges = []
  63. self.__bmp_rec = b''
  64. self.__show_formulas = 0
  65. self.__show_grid = 1
  66. self.__show_headers = 1
  67. self.__panes_frozen = 0
  68. self.show_zero_values = 1
  69. self.__auto_colour_grid = 1
  70. self.__cols_right_to_left = 0
  71. self.__show_outline = 1
  72. self.__remove_splits = 0
  73. # Multiple sheets can be selected, but only one can be active
  74. # (hold down Ctrl and click multiple tabs in the file in OOo)
  75. self.__selected = 0
  76. # "sheet_visible" should really be called "sheet_active"
  77. # and is 1 when this sheet is the sheet displayed when the file
  78. # is open. More than likely only one sheet should ever be set as
  79. # visible.
  80. # The same sheet should be specified in Workbook.active_sheet
  81. # (that way, both the WINDOW1 record in the book and the WINDOW2
  82. # records in each sheet will be in agreement)
  83. # The visibility of the sheet is found in the "visibility"
  84. # attribute obtained from the BOUNDSHEET record.
  85. self.__sheet_visible = 0
  86. self.__page_preview = 0
  87. self.__first_visible_row = 0
  88. self.__first_visible_col = 0
  89. self.__grid_colour = 0x40
  90. self.__preview_magn = 0 # use default (60%)
  91. self.__normal_magn = 0 # use default (100%)
  92. self.__scl_magn = None
  93. self.explicit_magn_setting = False
  94. self.visibility = 0 # from/to BOUNDSHEET record.
  95. self.__vert_split_pos = None
  96. self.__horz_split_pos = None
  97. self.__vert_split_first_visible = None
  98. self.__horz_split_first_visible = None
  99. # This is a caller-settable flag:
  100. self.split_position_units_are_twips = False
  101. # Default is False for backward compatibility with pyExcelerator
  102. # and previous versions of xlwt.
  103. # if panes_frozen:
  104. # vert/horz_split_pos are taken as number of rows/cols
  105. # else: # split
  106. # if split_position_units_are_twips:
  107. # vert/horz_split_pos are taken as number of twips
  108. # else:
  109. # vert/horz_split_pos are taken as
  110. # number of rows(cols) * default row(col) height (width) (i.e. 12.75 (8.43) somethings)
  111. # and converted to twips by approximate formulas
  112. # Callers who are copying an existing file should use
  113. # xlwt_worksheet.split_position_units_are_twips = True
  114. # because that's what's actually in the file.
  115. # There are 20 twips to a point. There are 72 points to an inch.
  116. self.__row_gut_width = 0
  117. self.__col_gut_height = 0
  118. self.__show_auto_page_breaks = 1
  119. self.__dialogue_sheet = 0
  120. self.__auto_style_outline = 0
  121. self.__outline_below = 0
  122. self.__outline_right = 0
  123. self.__fit_num_pages = 0
  124. self.__show_row_outline = 1
  125. self.__show_col_outline = 1
  126. self.__alt_expr_eval = 0
  127. self.__alt_formula_entries = 0
  128. self.__row_default_height = 0x00FF
  129. self.row_default_height_mismatch = 0
  130. self.row_default_hidden = 0
  131. self.row_default_space_above = 0
  132. self.row_default_space_below = 0
  133. self.__col_default_width = 0x0008
  134. self.__calc_mode = 1
  135. self.__calc_count = 0x0064
  136. self.__RC_ref_mode = 1
  137. self.__iterations_on = 0
  138. self.__delta = 0.001
  139. self.__save_recalc = 0
  140. self.__print_headers = 0
  141. self.__print_grid = 0
  142. self.__grid_set = 1
  143. self.__vert_page_breaks = []
  144. self.__horz_page_breaks = []
  145. self.__header_str = '&P'
  146. self.__footer_str = '&F'
  147. self.__print_centered_vert = 0
  148. self.__print_centered_horz = 1
  149. self.__left_margin = 0.3 #0.5
  150. self.__right_margin = 0.3 #0.5
  151. self.__top_margin = 0.61 #1.0
  152. self.__bottom_margin = 0.37 #1.0
  153. self.__paper_size_code = 9 # A4
  154. self.__print_scaling = 100
  155. self.__start_page_number = 1
  156. self.__fit_width_to_pages = 1
  157. self.__fit_height_to_pages = 1
  158. self.__print_in_rows = 1
  159. self.__portrait = 1
  160. self.__print_not_colour = 0
  161. self.__print_draft = 0
  162. self.__print_notes = 0
  163. self.__print_notes_at_end = 0
  164. self.__print_omit_errors = 0
  165. self.__print_hres = 0x012C # 300 dpi
  166. self.__print_vres = 0x012C # 300 dpi
  167. self.__header_margin = 0.1
  168. self.__footer_margin = 0.1
  169. self.__copies_num = 1
  170. self.__wnd_protect = 0
  171. self.__obj_protect = 0
  172. self.__protect = 0
  173. self.__scen_protect = 0
  174. self.__password = ''
  175. self.last_used_row = 0
  176. self.first_used_row = 65535
  177. self.last_used_col = 0
  178. self.first_used_col = 255
  179. self.row_tempfile = None
  180. self.__flushed_rows = {}
  181. self.__row_visible_levels = 0
  182. #################################################################
  183. ## Properties, "getters", "setters"
  184. #################################################################
  185. def set_name(self, value):
  186. self.__name = value
  187. def get_name(self):
  188. return self.__name
  189. name = property(get_name, set_name)
  190. #################################################################
  191. def get_parent(self):
  192. return self.__parent
  193. parent = property(get_parent)
  194. #################################################################
  195. def get_rows(self):
  196. return self.__rows
  197. rows = property(get_rows)
  198. #################################################################
  199. def get_cols(self):
  200. return self.__cols
  201. cols = property(get_cols)
  202. #################################################################
  203. def get_merged_ranges(self):
  204. return self.__merged_ranges
  205. merged_ranges = property(get_merged_ranges)
  206. #################################################################
  207. def get_bmp_rec(self):
  208. return self.__bmp_rec
  209. bmp_rec = property(get_bmp_rec)
  210. #################################################################
  211. def set_show_formulas(self, value):
  212. self.__show_formulas = int(value)
  213. def get_show_formulas(self):
  214. return bool(self.__show_formulas)
  215. show_formulas = property(get_show_formulas, set_show_formulas)
  216. #################################################################
  217. def set_show_grid(self, value):
  218. self.__show_grid = int(value)
  219. def get_show_grid(self):
  220. return bool(self.__show_grid)
  221. show_grid = property(get_show_grid, set_show_grid)
  222. #################################################################
  223. def set_show_headers(self, value):
  224. self.__show_headers = int(value)
  225. def get_show_headers(self):
  226. return bool(self.__show_headers)
  227. show_headers = property(get_show_headers, set_show_headers)
  228. #################################################################
  229. def set_panes_frozen(self, value):
  230. self.__panes_frozen = int(value)
  231. def get_panes_frozen(self):
  232. return bool(self.__panes_frozen)
  233. panes_frozen = property(get_panes_frozen, set_panes_frozen)
  234. #################################################################
  235. ### def set_show_empty_as_zero(self, value):
  236. ### self.__show_empty_as_zero = int(value)
  237. ### def get_show_empty_as_zero(self):
  238. ### return bool(self.__show_empty_as_zero)
  239. ### show_empty_as_zero = property(get_show_empty_as_zero, set_show_empty_as_zero)
  240. #################################################################
  241. def set_auto_colour_grid(self, value):
  242. self.__auto_colour_grid = int(value)
  243. def get_auto_colour_grid(self):
  244. return bool(self.__auto_colour_grid)
  245. auto_colour_grid = property(get_auto_colour_grid, set_auto_colour_grid)
  246. #################################################################
  247. def set_cols_right_to_left(self, value):
  248. self.__cols_right_to_left = int(value)
  249. def get_cols_right_to_left(self):
  250. return bool(self.__cols_right_to_left)
  251. cols_right_to_left = property(get_cols_right_to_left, set_cols_right_to_left)
  252. #################################################################
  253. def set_show_outline(self, value):
  254. self.__show_outline = int(value)
  255. def get_show_outline(self):
  256. return bool(self.__show_outline)
  257. show_outline = property(get_show_outline, set_show_outline)
  258. #################################################################
  259. def set_remove_splits(self, value):
  260. self.__remove_splits = int(value)
  261. def get_remove_splits(self):
  262. return bool(self.__remove_splits)
  263. remove_splits = property(get_remove_splits, set_remove_splits)
  264. #################################################################
  265. def set_selected(self, value):
  266. self.__selected = int(value)
  267. def get_selected(self):
  268. return bool(self.__selected)
  269. selected = property(get_selected, set_selected)
  270. #################################################################
  271. def set_sheet_visible(self, value):
  272. self.__sheet_visible = int(value)
  273. def get_sheet_visible(self):
  274. return bool(self.__sheet_visible)
  275. sheet_visible = property(get_sheet_visible, set_sheet_visible)
  276. #################################################################
  277. def set_page_preview(self, value):
  278. self.__page_preview = int(value)
  279. def get_page_preview(self):
  280. return bool(self.__page_preview)
  281. page_preview = property(get_page_preview, set_page_preview)
  282. #################################################################
  283. def set_first_visible_row(self, value):
  284. self.__first_visible_row = value
  285. def get_first_visible_row(self):
  286. return self.__first_visible_row
  287. first_visible_row = property(get_first_visible_row, set_first_visible_row)
  288. #################################################################
  289. def set_first_visible_col(self, value):
  290. self.__first_visible_col = value
  291. def get_first_visible_col(self):
  292. return self.__first_visible_col
  293. first_visible_col = property(get_first_visible_col, set_first_visible_col)
  294. #################################################################
  295. def set_grid_colour(self, value):
  296. self.__grid_colour = value
  297. def get_grid_colour(self):
  298. return self.__grid_colour
  299. grid_colour = property(get_grid_colour, set_grid_colour)
  300. #################################################################
  301. def set_preview_magn(self, value):
  302. self.__preview_magn = value
  303. def get_preview_magn(self):
  304. return self.__preview_magn
  305. preview_magn = property(get_preview_magn, set_preview_magn)
  306. #################################################################
  307. def set_normal_magn(self, value):
  308. self.__normal_magn = value
  309. def get_normal_magn(self):
  310. return self.__normal_magn
  311. normal_magn = property(get_normal_magn, set_normal_magn)
  312. #################################################################
  313. def set_scl_magn(self, value):
  314. self.__scl_magn = value
  315. def get_scl_magn(self):
  316. return self.__scl_magn
  317. scl_magn = property(get_scl_magn, set_scl_magn)
  318. #################################################################
  319. def set_vert_split_pos(self, value):
  320. self.__vert_split_pos = abs(value)
  321. def get_vert_split_pos(self):
  322. return self.__vert_split_pos
  323. vert_split_pos = property(get_vert_split_pos, set_vert_split_pos)
  324. #################################################################
  325. def set_horz_split_pos(self, value):
  326. self.__horz_split_pos = abs(value)
  327. def get_horz_split_pos(self):
  328. return self.__horz_split_pos
  329. horz_split_pos = property(get_horz_split_pos, set_horz_split_pos)
  330. #################################################################
  331. def set_vert_split_first_visible(self, value):
  332. self.__vert_split_first_visible = abs(value)
  333. def get_vert_split_first_visible(self):
  334. return self.__vert_split_first_visible
  335. vert_split_first_visible = property(get_vert_split_first_visible, set_vert_split_first_visible)
  336. #################################################################
  337. def set_horz_split_first_visible(self, value):
  338. self.__horz_split_first_visible = abs(value)
  339. def get_horz_split_first_visible(self):
  340. return self.__horz_split_first_visible
  341. horz_split_first_visible = property(get_horz_split_first_visible, set_horz_split_first_visible)
  342. #################################################################
  343. #def set_row_gut_width(self, value):
  344. # self.__row_gut_width = value
  345. #
  346. #def get_row_gut_width(self):
  347. # return self.__row_gut_width
  348. #
  349. #row_gut_width = property(get_row_gut_width, set_row_gut_width)
  350. #
  351. #################################################################
  352. #
  353. #def set_col_gut_height(self, value):
  354. # self.__col_gut_height = value
  355. #
  356. #def get_col_gut_height(self):
  357. # return self.__col_gut_height
  358. #
  359. #col_gut_height = property(get_col_gut_height, set_col_gut_height)
  360. #
  361. #################################################################
  362. def set_show_auto_page_breaks(self, value):
  363. self.__show_auto_page_breaks = int(value)
  364. def get_show_auto_page_breaks(self):
  365. return bool(self.__show_auto_page_breaks)
  366. show_auto_page_breaks = property(get_show_auto_page_breaks, set_show_auto_page_breaks)
  367. #################################################################
  368. def set_dialogue_sheet(self, value):
  369. self.__dialogue_sheet = int(value)
  370. def get_dialogue_sheet(self):
  371. return bool(self.__dialogue_sheet)
  372. dialogue_sheet = property(get_dialogue_sheet, set_dialogue_sheet)
  373. #################################################################
  374. def set_auto_style_outline(self, value):
  375. self.__auto_style_outline = int(value)
  376. def get_auto_style_outline(self):
  377. return bool(self.__auto_style_outline)
  378. auto_style_outline = property(get_auto_style_outline, set_auto_style_outline)
  379. #################################################################
  380. def set_outline_below(self, value):
  381. self.__outline_below = int(value)
  382. def get_outline_below(self):
  383. return bool(self.__outline_below)
  384. outline_below = property(get_outline_below, set_outline_below)
  385. #################################################################
  386. def set_outline_right(self, value):
  387. self.__outline_right = int(value)
  388. def get_outline_right(self):
  389. return bool(self.__outline_right)
  390. outline_right = property(get_outline_right, set_outline_right)
  391. #################################################################
  392. def set_fit_num_pages(self, value):
  393. self.__fit_num_pages = value
  394. def get_fit_num_pages(self):
  395. return self.__fit_num_pages
  396. fit_num_pages = property(get_fit_num_pages, set_fit_num_pages)
  397. #################################################################
  398. def set_show_row_outline(self, value):
  399. self.__show_row_outline = int(value)
  400. def get_show_row_outline(self):
  401. return bool(self.__show_row_outline)
  402. show_row_outline = property(get_show_row_outline, set_show_row_outline)
  403. #################################################################
  404. def set_show_col_outline(self, value):
  405. self.__show_col_outline = int(value)
  406. def get_show_col_outline(self):
  407. return bool(self.__show_col_outline)
  408. show_col_outline = property(get_show_col_outline, set_show_col_outline)
  409. #################################################################
  410. def set_alt_expr_eval(self, value):
  411. self.__alt_expr_eval = int(value)
  412. def get_alt_expr_eval(self):
  413. return bool(self.__alt_expr_eval)
  414. alt_expr_eval = property(get_alt_expr_eval, set_alt_expr_eval)
  415. #################################################################
  416. def set_alt_formula_entries(self, value):
  417. self.__alt_formula_entries = int(value)
  418. def get_alt_formula_entries(self):
  419. return bool(self.__alt_formula_entries)
  420. alt_formula_entries = property(get_alt_formula_entries, set_alt_formula_entries)
  421. #################################################################
  422. def set_row_default_height(self, value):
  423. self.__row_default_height = value
  424. def get_row_default_height(self):
  425. return self.__row_default_height
  426. row_default_height = property(get_row_default_height, set_row_default_height)
  427. #################################################################
  428. def set_col_default_width(self, value):
  429. self.__col_default_width = value
  430. def get_col_default_width(self):
  431. return self.__col_default_width
  432. col_default_width = property(get_col_default_width, set_col_default_width)
  433. #################################################################
  434. def set_calc_mode(self, value):
  435. self.__calc_mode = value & 0x03
  436. def get_calc_mode(self):
  437. return self.__calc_mode
  438. calc_mode = property(get_calc_mode, set_calc_mode)
  439. #################################################################
  440. def set_calc_count(self, value):
  441. self.__calc_count = value
  442. def get_calc_count(self):
  443. return self.__calc_count
  444. calc_count = property(get_calc_count, set_calc_count)
  445. #################################################################
  446. def set_RC_ref_mode(self, value):
  447. self.__RC_ref_mode = int(value)
  448. def get_RC_ref_mode(self):
  449. return bool(self.__RC_ref_mode)
  450. RC_ref_mode = property(get_RC_ref_mode, set_RC_ref_mode)
  451. #################################################################
  452. def set_iterations_on(self, value):
  453. self.__iterations_on = int(value)
  454. def get_iterations_on(self):
  455. return bool(self.__iterations_on)
  456. iterations_on = property(get_iterations_on, set_iterations_on)
  457. #################################################################
  458. def set_delta(self, value):
  459. self.__delta = value
  460. def get_delta(self):
  461. return self.__delta
  462. delta = property(get_delta, set_delta)
  463. #################################################################
  464. def set_save_recalc(self, value):
  465. self.__save_recalc = int(value)
  466. def get_save_recalc(self):
  467. return bool(self.__save_recalc)
  468. save_recalc = property(get_save_recalc, set_save_recalc)
  469. #################################################################
  470. def set_print_headers(self, value):
  471. self.__print_headers = int(value)
  472. def get_print_headers(self):
  473. return bool(self.__print_headers)
  474. print_headers = property(get_print_headers, set_print_headers)
  475. #################################################################
  476. def set_print_grid(self, value):
  477. self.__print_grid = int(value)
  478. def get_print_grid(self):
  479. return bool(self.__print_grid)
  480. print_grid = property(get_print_grid, set_print_grid)
  481. #################################################################
  482. #
  483. #def set_grid_set(self, value):
  484. # self.__grid_set = int(value)
  485. #
  486. #def get_grid_set(self):
  487. # return bool(self.__grid_set)
  488. #
  489. #grid_set = property(get_grid_set, set_grid_set)
  490. #
  491. #################################################################
  492. def set_vert_page_breaks(self, value):
  493. self.__vert_page_breaks = value
  494. def get_vert_page_breaks(self):
  495. return self.__vert_page_breaks
  496. vert_page_breaks = property(get_vert_page_breaks, set_vert_page_breaks)
  497. #################################################################
  498. def set_horz_page_breaks(self, value):
  499. self.__horz_page_breaks = value
  500. def get_horz_page_breaks(self):
  501. return self.__horz_page_breaks
  502. horz_page_breaks = property(get_horz_page_breaks, set_horz_page_breaks)
  503. #################################################################
  504. def set_header_str(self, value):
  505. if isinstance(value, str):
  506. value = unicode(value, self.__parent.encoding)
  507. self.__header_str = value
  508. def get_header_str(self):
  509. return self.__header_str
  510. header_str = property(get_header_str, set_header_str)
  511. #################################################################
  512. def set_footer_str(self, value):
  513. if isinstance(value, str):
  514. value = unicode(value, self.__parent.encoding)
  515. self.__footer_str = value
  516. def get_footer_str(self):
  517. return self.__footer_str
  518. footer_str = property(get_footer_str, set_footer_str)
  519. #################################################################
  520. def set_print_centered_vert(self, value):
  521. self.__print_centered_vert = int(value)
  522. def get_print_centered_vert(self):
  523. return bool(self.__print_centered_vert)
  524. print_centered_vert = property(get_print_centered_vert, set_print_centered_vert)
  525. #################################################################
  526. def set_print_centered_horz(self, value):
  527. self.__print_centered_horz = int(value)
  528. def get_print_centered_horz(self):
  529. return bool(self.__print_centered_horz)
  530. print_centered_horz = property(get_print_centered_horz, set_print_centered_horz)
  531. #################################################################
  532. def set_left_margin(self, value):
  533. self.__left_margin = value
  534. def get_left_margin(self):
  535. return self.__left_margin
  536. left_margin = property(get_left_margin, set_left_margin)
  537. #################################################################
  538. def set_right_margin(self, value):
  539. self.__right_margin = value
  540. def get_right_margin(self):
  541. return self.__right_margin
  542. right_margin = property(get_right_margin, set_right_margin)
  543. #################################################################
  544. def set_top_margin(self, value):
  545. self.__top_margin = value
  546. def get_top_margin(self):
  547. return self.__top_margin
  548. top_margin = property(get_top_margin, set_top_margin)
  549. #################################################################
  550. def set_bottom_margin(self, value):
  551. self.__bottom_margin = value
  552. def get_bottom_margin(self):
  553. return self.__bottom_margin
  554. bottom_margin = property(get_bottom_margin, set_bottom_margin)
  555. #################################################################
  556. def set_paper_size_code(self, value):
  557. self.__paper_size_code = value
  558. def get_paper_size_code(self):
  559. return self.__paper_size_code
  560. paper_size_code = property(get_paper_size_code, set_paper_size_code)
  561. #################################################################
  562. def set_print_scaling(self, value):
  563. self.__print_scaling = value
  564. def get_print_scaling(self):
  565. return self.__print_scaling
  566. print_scaling = property(get_print_scaling, set_print_scaling)
  567. #################################################################
  568. def set_start_page_number(self, value):
  569. self.__start_page_number = value
  570. def get_start_page_number(self):
  571. return self.__start_page_number
  572. start_page_number = property(get_start_page_number, set_start_page_number)
  573. #################################################################
  574. def set_fit_width_to_pages(self, value):
  575. self.__fit_width_to_pages = value
  576. def get_fit_width_to_pages(self):
  577. return self.__fit_width_to_pages
  578. fit_width_to_pages = property(get_fit_width_to_pages, set_fit_width_to_pages)
  579. #################################################################
  580. def set_fit_height_to_pages(self, value):
  581. self.__fit_height_to_pages = value
  582. def get_fit_height_to_pages(self):
  583. return self.__fit_height_to_pages
  584. fit_height_to_pages = property(get_fit_height_to_pages, set_fit_height_to_pages)
  585. #################################################################
  586. def set_print_in_rows(self, value):
  587. self.__print_in_rows = int(value)
  588. def get_print_in_rows(self):
  589. return bool(self.__print_in_rows)
  590. print_in_rows = property(get_print_in_rows, set_print_in_rows)
  591. #################################################################
  592. def set_portrait(self, value):
  593. self.__portrait = int(value)
  594. def get_portrait(self):
  595. return bool(self.__portrait)
  596. portrait = property(get_portrait, set_portrait)
  597. #################################################################
  598. def set_print_colour(self, value):
  599. self.__print_not_colour = int(not value)
  600. def get_print_colour(self):
  601. return not bool(self.__print_not_colour)
  602. print_colour = property(get_print_colour, set_print_colour)
  603. #################################################################
  604. def set_print_draft(self, value):
  605. self.__print_draft = int(value)
  606. def get_print_draft(self):
  607. return bool(self.__print_draft)
  608. print_draft = property(get_print_draft, set_print_draft)
  609. #################################################################
  610. def set_print_notes(self, value):
  611. self.__print_notes = int(value)
  612. def get_print_notes(self):
  613. return bool(self.__print_notes)
  614. print_notes = property(get_print_notes, set_print_notes)
  615. #################################################################
  616. def set_print_notes_at_end(self, value):
  617. self.__print_notes_at_end = int(value)
  618. def get_print_notes_at_end(self):
  619. return bool(self.__print_notes_at_end)
  620. print_notes_at_end = property(get_print_notes_at_end, set_print_notes_at_end)
  621. #################################################################
  622. def set_print_omit_errors(self, value):
  623. self.__print_omit_errors = int(value)
  624. def get_print_omit_errors(self):
  625. return bool(self.__print_omit_errors)
  626. print_omit_errors = property(get_print_omit_errors, set_print_omit_errors)
  627. #################################################################
  628. def set_print_hres(self, value):
  629. self.__print_hres = value
  630. def get_print_hres(self):
  631. return self.__print_hres
  632. print_hres = property(get_print_hres, set_print_hres)
  633. #################################################################
  634. def set_print_vres(self, value):
  635. self.__print_vres = value
  636. def get_print_vres(self):
  637. return self.__print_vres
  638. print_vres = property(get_print_vres, set_print_vres)
  639. #################################################################
  640. def set_header_margin(self, value):
  641. self.__header_margin = value
  642. def get_header_margin(self):
  643. return self.__header_margin
  644. header_margin = property(get_header_margin, set_header_margin)
  645. #################################################################
  646. def set_footer_margin(self, value):
  647. self.__footer_margin = value
  648. def get_footer_margin(self):
  649. return self.__footer_margin
  650. footer_margin = property(get_footer_margin, set_footer_margin)
  651. #################################################################
  652. def set_copies_num(self, value):
  653. self.__copies_num = value
  654. def get_copies_num(self):
  655. return self.__copies_num
  656. copies_num = property(get_copies_num, set_copies_num)
  657. ##################################################################
  658. def set_wnd_protect(self, value):
  659. self.__wnd_protect = int(value)
  660. def get_wnd_protect(self):
  661. return bool(self.__wnd_protect)
  662. wnd_protect = property(get_wnd_protect, set_wnd_protect)
  663. #################################################################
  664. def set_obj_protect(self, value):
  665. self.__obj_protect = int(value)
  666. def get_obj_protect(self):
  667. return bool(self.__obj_protect)
  668. obj_protect = property(get_obj_protect, set_obj_protect)
  669. #################################################################
  670. def set_protect(self, value):
  671. self.__protect = int(value)
  672. def get_protect(self):
  673. return bool(self.__protect)
  674. protect = property(get_protect, set_protect)
  675. #################################################################
  676. def set_scen_protect(self, value):
  677. self.__scen_protect = int(value)
  678. def get_scen_protect(self):
  679. return bool(self.__scen_protect)
  680. scen_protect = property(get_scen_protect, set_scen_protect)
  681. #################################################################
  682. def set_password(self, value):
  683. self.__password = value
  684. def get_password(self):
  685. return self.__password
  686. password = property(get_password, set_password)
  687. ##################################################################
  688. ## Methods
  689. ##################################################################
  690. def get_parent(self):
  691. return self.__parent
  692. def write(self, r, c, label="", style=Style.default_style):
  693. """
  694. This method is used to write a cell to a :class:`Worksheet`.
  695. :param r:
  696. The zero-relative number of the row in the worksheet to which
  697. the cell should be written.
  698. :param c:
  699. The zero-relative number of the column in the worksheet to which
  700. the cell should be written.
  701. :param label:
  702. The data value to be written.
  703. An :class:`int`, :class:`long`, or
  704. :class:`~decimal.Decimal` instance is converted to :class:`float`.
  705. A :class:`unicode` instance is written as is. A :class:`bytes`
  706. instance is converted to :class:`unicode` using the
  707. encoding, which defaults to ``ascii``, specified when the
  708. :class:`Workbook` instance was created.
  709. A :class:`~datetime.datetime`, :class:`~datetime.date` or
  710. :class:`~datetime.time` instance is converted into Excel date format
  711. (a float representing the number of days since (typically)
  712. ``1899-12-31T00:00:00``, under the pretence that
  713. 1900 was a leap year).
  714. A :class:`bool` instance will show up as ``TRUE`` or ``FALSE`` in
  715. Excel.
  716. ``None`` causes the cell to be blank: no data, only formatting.
  717. An :class:`xlwt.Formula` instance causes an Excel formula to be
  718. written.
  719. :param style:
  720. A style, also known as an XF (extended format), is an
  721. :class:`~xlwt.Style.XFStyle` object, which encapsulates the
  722. formatting applied to the cell and its contents.
  723. :class:`~xlwt.Style.XFStyle` objects are best set up using the
  724. :func:`~xlwt.Style.easyxf` function. They may also be set up by
  725. setting attributes in :class:`Alignment`, :class:`Borders`,
  726. :class:`Pattern`, :class:`Font` and :class:`Protection` objects then
  727. setting those objects and a format string as attributes of an
  728. :class:`~xlwt.Style.XFStyle` object.
  729. """
  730. self.row(r).write(c, label, style)
  731. def write_rich_text(self, r, c, rich_text_list, style=Style.default_style):
  732. self.row(r).set_cell_rich_text(c, rich_text_list, style)
  733. def merge(self, r1, r2, c1, c2, style=Style.default_style):
  734. # Stand-alone merge of previously written cells.
  735. # Problems: (1) style to be used should be existing style of
  736. # the top-left cell, not an arg.
  737. # (2) should ensure that any previous data value in
  738. # non-top-left cells is nobbled.
  739. # Note: if a cell is set by a data record then later
  740. # is referenced by a [MUL]BLANK record, Excel will blank
  741. # out the cell on the screen, but OOo & Gnu will not
  742. # blank it out. Need to do something better than writing
  743. # multiple records. In the meantime, avoid this method and use
  744. # write_merge() instead.
  745. if c2 > c1:
  746. self.row(r1).write_blanks(c1 + 1, c2, style)
  747. for r in range(r1+1, r2+1):
  748. self.row(r).write_blanks(c1, c2, style)
  749. self.__merged_ranges.append((r1, r2, c1, c2))
  750. def write_merge(self, r1, r2, c1, c2, label="", style=Style.default_style):
  751. assert 0 <= c1 <= c2 <= 255
  752. assert 0 <= r1 <= r2 <= 65535
  753. self.write(r1, c1, label, style)
  754. if c2 > c1:
  755. self.row(r1).write_blanks(c1 + 1, c2, style) # skip (r1, c1)
  756. for r in range(r1+1, r2+1):
  757. self.row(r).write_blanks(c1, c2, style)
  758. self.__merged_ranges.append((r1, r2, c1, c2))
  759. def insert_bitmap(self, filename, row, col, x = 0, y = 0, scale_x = 1, scale_y = 1):
  760. bmp = Bitmap.ImDataBmpRecord(filename)
  761. obj = Bitmap.ObjBmpRecord(row, col, self, bmp, x, y, scale_x, scale_y)
  762. self.__bmp_rec += obj.get() + bmp.get()
  763. def insert_bitmap_data(self, data, row, col, x = 0, y = 0, scale_x = 1, scale_y = 1):
  764. bmp = Bitmap.ImRawDataBmpRecord(data)
  765. obj = Bitmap.ObjBmpRecord(row, col, self, bmp, x, y, scale_x, scale_y)
  766. self.__bmp_rec += obj.get() + bmp.get()
  767. def col(self, indx):
  768. if indx not in self.__cols:
  769. self.__cols[indx] = self.Column(indx, self)
  770. return self.__cols[indx]
  771. def row(self, indx):
  772. if indx not in self.__rows:
  773. if indx in self.__flushed_rows:
  774. raise Exception("Attempt to reuse row index %d of sheet %r after flushing" % (indx, self.__name))
  775. self.__rows[indx] = self.Row(indx, self)
  776. if indx > self.last_used_row:
  777. self.last_used_row = indx
  778. if indx < self.first_used_row:
  779. self.first_used_row = indx
  780. return self.__rows[indx]
  781. def row_height(self, row): # in pixels
  782. if row in self.__rows:
  783. return self.__rows[row].get_height_in_pixels()
  784. else:
  785. return 17
  786. def col_width(self, col): # in pixels
  787. if col in self.__cols:
  788. return self.__cols[col].width_in_pixels()
  789. else:
  790. return 64
  791. ##################################################################
  792. ## BIFF records generation
  793. ##################################################################
  794. def __bof_rec(self):
  795. return BIFFRecords.Biff8BOFRecord(BIFFRecords.Biff8BOFRecord.WORKSHEET).get()
  796. def __update_row_visible_levels(self):
  797. if self.__rows:
  798. temp = max(self.__rows[r].level for r in self.__rows) + 1
  799. self.__row_visible_levels = max(temp, self.__row_visible_levels)
  800. def __guts_rec(self):
  801. self.__update_row_visible_levels()
  802. col_visible_levels = 0
  803. if len(self.__cols) != 0:
  804. col_visible_levels = max(self.__cols[c].level for c in self.__cols) + 1
  805. return BIFFRecords.GutsRecord(
  806. self.__row_gut_width, self.__col_gut_height, self.__row_visible_levels, col_visible_levels).get()
  807. def __defaultrowheight_rec(self):
  808. options = 0x0000
  809. options |= (self.row_default_height_mismatch & 1) << 0
  810. options |= (self.row_default_hidden & 1) << 1
  811. options |= (self.row_default_space_above & 1) << 2
  812. options |= (self.row_default_space_below & 1) << 3
  813. defht = self.__row_default_height
  814. return BIFFRecords.DefaultRowHeightRecord(options, defht).get()
  815. def __wsbool_rec(self):
  816. options = 0x00
  817. options |= (self.__show_auto_page_breaks & 0x01) << 0
  818. options |= (self.__dialogue_sheet & 0x01) << 4
  819. options |= (self.__auto_style_outline & 0x01) << 5
  820. options |= (self.__outline_below & 0x01) << 6
  821. options |= (self.__outline_right & 0x01) << 7
  822. options |= (self.__fit_num_pages & 0x01) << 8
  823. options |= (self.__show_row_outline & 0x01) << 10
  824. options |= (self.__show_col_outline & 0x01) << 11
  825. options |= (self.__alt_expr_eval & 0x01) << 14
  826. options |= (self.__alt_formula_entries & 0x01) << 15
  827. return BIFFRecords.WSBoolRecord(options).get()
  828. def __eof_rec(self):
  829. return BIFFRecords.EOFRecord().get()
  830. def __colinfo_rec(self):
  831. result = b''
  832. for col in self.__cols:
  833. result += self.__cols[col].get_biff_record()
  834. return result
  835. def __dimensions_rec(self):
  836. return BIFFRecords.DimensionsRecord(
  837. self.first_used_row, self.last_used_row,
  838. self.first_used_col, self.last_used_col
  839. ).get()
  840. def __window2_rec(self):
  841. # Appends SCL record.
  842. options = 0
  843. options |= (self.__show_formulas & 0x01) << 0
  844. options |= (self.__show_grid & 0x01) << 1
  845. options |= (self.__show_headers & 0x01) << 2
  846. options |= (self.__panes_frozen & 0x01) << 3
  847. options |= (self.show_zero_values & 0x01) << 4
  848. options |= (self.__auto_colour_grid & 0x01) << 5
  849. options |= (self.__cols_right_to_left & 0x01) << 6
  850. options |= (self.__show_outline & 0x01) << 7
  851. options |= (self.__remove_splits & 0x01) << 8
  852. options |= (self.__selected & 0x01) << 9
  853. options |= (self.__sheet_visible & 0x01) << 10
  854. options |= (self.__page_preview & 0x01) << 11
  855. if self.explicit_magn_setting:
  856. # Experimentation: caller can set the scl magn.
  857. # None -> no SCL record written
  858. # Otherwise 10 <= scl_magn <= 400 or scl_magn == 0
  859. # Note: value 0 means use 100 for normal view, 60 for page break preview
  860. # BREAKING NEWS: Excel interprets scl_magn = 0 very literally, your
  861. # sheet appears like a tiny dot on the screen
  862. scl_magn = self.__scl_magn
  863. else:
  864. if self.__page_preview:
  865. scl_magn = self.__preview_magn
  866. magn_default = 60
  867. else:
  868. scl_magn = self.__normal_magn
  869. magn_default = 100
  870. if scl_magn == magn_default or scl_magn == 0:
  871. # Emulate what we think MS does
  872. scl_magn = None # don't write an SCL record
  873. return BIFFRecords.Window2Record(
  874. options, self.__first_visible_row, self.__first_visible_col,
  875. self.__grid_colour,
  876. self.__preview_magn, self.__normal_magn, scl_magn).get()
  877. def __panes_rec(self):
  878. if self.__vert_split_pos is None and self.__horz_split_pos is None:
  879. return b""
  880. if self.__vert_split_pos is None:
  881. self.__vert_split_pos = 0
  882. if self.__horz_split_pos is None:
  883. self.__horz_split_pos = 0
  884. if self.__panes_frozen:
  885. if self.__vert_split_first_visible is None:
  886. self.__vert_split_first_visible = self.__vert_split_pos
  887. if self.__horz_split_first_visible is None:
  888. self.__horz_split_first_visible = self.__horz_split_pos
  889. # when frozen, the active pane has to be specifically set:
  890. if self.__vert_split_pos > 0 and self.__horz_split_pos > 0:
  891. active_pane = 0
  892. elif self.__vert_split_pos > 0 and self.__horz_split_pos == 0:
  893. active_pane = 1
  894. elif self.__vert_split_pos == 0 and self.__horz_split_pos > 0:
  895. active_pane = 2
  896. else:
  897. active_pane = 3
  898. else:
  899. if self.__vert_split_first_visible is None:
  900. self.__vert_split_first_visible = 0
  901. if self.__horz_split_first_visible is None:
  902. self.__horz_split_first_visible = 0
  903. if not self.split_position_units_are_twips:
  904. # inspired by pyXLWriter
  905. if self.__horz_split_pos > 0:
  906. self.__horz_split_pos = 20 * self.__horz_split_pos + 255
  907. if self.__vert_split_pos > 0:
  908. self.__vert_split_pos = 113.879 * self.__vert_split_pos + 390
  909. # when split, the active pain can be set as required:
  910. active_pane = self.active_pane
  911. result = BIFFRecords.PanesRecord(*map(int, (
  912. self.__vert_split_pos,
  913. self.__horz_split_pos,
  914. self.__horz_split_first_visible,
  915. self.__vert_split_first_visible,
  916. active_pane
  917. ))).get()
  918. return result
  919. def __row_blocks_rec(self):
  920. result = []
  921. for row in itervalues(self.__rows):
  922. result.append(row.get_row_biff_data())
  923. result.append(row.get_cells_biff_data())
  924. return b''.join(result)
  925. def __merged_rec(self):
  926. return BIFFRecords.MergedCellsRecord(self.__merged_ranges).get()
  927. def __bitmaps_rec(self):
  928. return self.__bmp_rec
  929. def __calc_settings_rec(self):
  930. result = b''
  931. result += BIFFRecords.CalcModeRecord(self.__calc_mode & 0x01).get()
  932. result += BIFFRecords.CalcCountRecord(self.__calc_count & 0xFFFF).get()
  933. result += BIFFRecords.RefModeRecord(self.__RC_ref_mode & 0x01).get()
  934. result += BIFFRecords.IterationRecord(self.__iterations_on & 0x01).get()
  935. result += BIFFRecords.DeltaRecord(self.__delta).get()
  936. result += BIFFRecords.SaveRecalcRecord(self.__save_recalc & 0x01).get()
  937. return result
  938. def __print_settings_rec(self):
  939. result = b''
  940. result += BIFFRecords.PrintHeadersRecord(self.__print_headers).get()
  941. result += BIFFRecords.PrintGridLinesRecord(self.__print_grid).get()
  942. result += BIFFRecords.GridSetRecord(self.__grid_set).get()
  943. result += BIFFRecords.HorizontalPageBreaksRecord(self.__horz_page_breaks).get()
  944. result += BIFFRecords.VerticalPageBreaksRecord(self.__vert_page_breaks).get()
  945. result += BIFFRecords.HeaderRecord(self.__header_str).get()
  946. result += BIFFRecords.FooterRecord(self.__footer_str).get()
  947. result += BIFFRecords.HCenterRecord(self.__print_centered_horz).get()
  948. result += BIFFRecords.VCenterRecord(self.__print_centered_vert).get()
  949. result += BIFFRecords.LeftMarginRecord(self.__left_margin).get()
  950. result += BIFFRecords.RightMarginRecord(self.__right_margin).get()
  951. result += BIFFRecords.TopMarginRecord(self.__top_margin).get()
  952. result += BIFFRecords.BottomMarginRecord(self.__bottom_margin).get()
  953. setup_page_options = (self.__print_in_rows & 0x01) << 0
  954. setup_page_options |= (self.__portrait & 0x01) << 1
  955. setup_page_options |= (0x00 & 0x01) << 2
  956. setup_page_options |= (self.__print_not_colour & 0x01) << 3
  957. setup_page_options |= (self.__print_draft & 0x01) << 4
  958. setup_page_options |= (self.__print_notes & 0x01) << 5
  959. setup_page_options |= (0x00 & 0x01) << 6
  960. setup_page_options |= (0x01 & 0x01) << 7
  961. setup_page_options |= (self.__print_notes_at_end & 0x01) << 9
  962. setup_page_options |= (self.__print_omit_errors & 0x03) << 10
  963. result += BIFFRecords.SetupPageRecord(self.__paper_size_code,
  964. self.__print_scaling,
  965. self.__start_page_number,
  966. self.__fit_width_to_pages,
  967. self.__fit_height_to_pages,
  968. setup_page_options,
  969. self.__print_hres,
  970. self.__print_vres,
  971. self.__header_margin,
  972. self.__footer_margin,
  973. self.__copies_num).get()
  974. return result
  975. def __protection_rec(self):
  976. result = b''
  977. result += BIFFRecords.ProtectRecord(self.__protect).get()
  978. result += BIFFRecords.ScenProtectRecord(self.__scen_protect).get()
  979. result += BIFFRecords.WindowProtectRecord(self.__wnd_protect).get()
  980. result += BIFFRecords.ObjectProtectRecord(self.__obj_protect).get()
  981. result += BIFFRecords.PasswordRecord(self.__password).get()
  982. return result
  983. def get_biff_data(self):
  984. result = [
  985. self.__bof_rec(),
  986. self.__calc_settings_rec(),
  987. self.__guts_rec(),
  988. self.__defaultrowheight_rec(),
  989. self.__wsbool_rec(),
  990. self.__colinfo_rec(),
  991. self.__dimensions_rec(),
  992. self.__print_settings_rec(),
  993. self.__protection_rec(),
  994. ]
  995. if self.row_tempfile:
  996. self.row_tempfile.flush()
  997. self.row_tempfile.seek(0)
  998. result.append(self.row_tempfile.read())
  999. self.row_tempfile.seek(0, 2) # to EOF
  1000. # Above seek() is necessary to avoid a spurious IOError
  1001. # with Errno 0 if the caller continues on writing rows
  1002. # and flushing row data after the save().
  1003. # See https://bugs.python.org/issue3207
  1004. result.extend([
  1005. self.__row_blocks_rec(),
  1006. self.__merged_rec(),
  1007. self.__bitmaps_rec(),
  1008. self.__window2_rec(),
  1009. self.__panes_rec(),
  1010. self.__eof_rec(),
  1011. ])
  1012. return b''.join(result)
  1013. def flush_row_data(self):
  1014. if self.row_tempfile is None:
  1015. self.row_tempfile = tempfile.TemporaryFile()
  1016. self.row_tempfile.write(self.__row_blocks_rec())
  1017. for rowx in self.__rows:
  1018. self.__flushed_rows[rowx] = 1
  1019. self.__update_row_visible_levels()
  1020. self.__rows = {}