图片有多少行?
我想在这里展示我的发现以即兴发挥结果。
int row=1;
lxw_image_options options = {.x_offset = 0, .y_offset = 0};
worksheet_insert_image_opt(worksheet, row, 2,"logo.png", &options);
row+=(options.height/worksheet->default_row_pixels);
在这里,我使用变量options.height来计算图片包含多少行。libxlsxwriter 确实从图像文件中读取了高度(以像素为单位)。它仅将 struct variable选项用于读取初始化变量,它永远不会在集合中写入任何内容。但我通过添加行user_options->height=options->height; 在 worksheet.c 的函数worksheet_insert_image_opt中。
lxw_error worksheet_insert_image_opt(lxw_worksheet *self,
lxw_row_t row_num, lxw_col_t col_num,
const char *filename,
lxw_image_options *user_options)
{
FILE *image_stream;
char *short_name;
lxw_image_options *options;
if (!filename) {
LXW_WARN("worksheet_insert_image()/_opt(): "
"filename must be specified.");
return LXW_ERROR_NULL_PARAMETER_IGNORED;
}
/* Check that the image file exists and can be opened. */
image_stream = fopen(filename, "rb");
if (!image_stream) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"file doesn't exist or can't be opened: %s.",
filename);
return LXW_ERROR_PARAMETER_VALIDATION;
}
/* Get the filename from the full path to add to the Drawing object. */
short_name = lxw_basename(filename);
if (!short_name) {
LXW_WARN_FORMAT1("worksheet_insert_image()/_opt(): "
"couldn't get basename for file: %s.", filename);
fclose(image_stream);
return LXW_ERROR_PARAMETER_VALIDATION;
}
/* Create a new object to hold the image options. */
options = calloc(1, sizeof(lxw_image_options));
if (!options) {
fclose(image_stream);
return LXW_ERROR_MEMORY_MALLOC_FAILED;
}
if (user_options) {
memcpy(options, user_options, sizeof(lxw_image_options));
options->url = lxw_strdup(user_options->url);
options->tip = lxw_strdup(user_options->tip);
}
/* Copy other options or set defaults. */
options->filename = lxw_strdup(filename);
options->short_name = lxw_strdup(short_name);
options->stream = image_stream;
options->row = row_num;
options->col = col_num;
if (!options->x_scale)
options->x_scale = 1;
if (!options->y_scale)
options->y_scale = 1;
if (_get_image_properties(options) == LXW_NO_ERROR) {
user_options->height=options->height;
STAILQ_INSERT_TAIL(self->image_data, options, list_pointers);
return LXW_NO_ERROR;
}
else {
free(options);
return LXW_ERROR_IMAGE_DIMENSIONS;
}
}
这就是我计算行的方式。如果有更好的方法请告诉我。